I read 576.73 however it is displayed as 576.729980! [duplicate]

0

Problem

The source code is only to return the amount of banknotes and coins based on an input value. However, I'm having trouble manipulating the value of the aux variable. In the example shown, the input value is 576.73 . But, as you can see, the aux variable has a loss and this is making my program run erroneously.

Code

#include <stdio.h>
#include <math.h>

int main()
{
   float N;
   float aux;

   scanf("%f", &N);

   aux = N;
   printf("aux = %f\n", aux);

   printf("NOTAS:\n");
   if (aux / 100)
   {
      aux = aux / 100;
      printf("%d nota(s) de R$ 100.00\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 100;
   }
   else
   {
      printf("0 nota(s) de R$ 100.00\n");
   }
   if (aux / 50)
   {
      aux = aux / 50;
      printf("%d nota(s) de R$ 50.00\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 50;
   }
   else
   {
      printf("0 nota(s) de R$ 50.00\n");
   }
   if (aux / 20)
   {
      aux = aux / 20;
      printf("%d nota(s) de R$ 20.00\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 20;
   }
   else
   {
      printf("0 nota(s) de R$ 20.00\n");
   }
   if (aux / 10)
   {
      aux = aux / 10;
      printf("%d nota(s) de R$ 10.00\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 10;
   }
   else
   {
      printf("0 nota(s) de R$ 10.00\n");
   }
   if (aux / 5)
   {
      aux = aux / 5;
      printf("%d nota(s) de R$ 5.00\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 5;
   }
   else
   {
      printf("0 nota(s) de R$ 5.00\n");
   }
   if (aux / 2)
   {
      aux = aux / 2;
      printf("%d nota(s) de R$ 2.00\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 2;
   }
   else
   {
      printf("0 nota(s) de R$ 2.00\n");
   }

   printf("MOEDAS:\n");
   if (aux / 1)
   {
      aux = aux / 1;
      printf("%d moeda(s) de R$ 1.00\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 1;
   }
   else
   {
      printf("0 moeda(s) de R$ 1.00\n");
   }
   if (aux / 0.5)
   {
      aux = aux / 0.5;
      printf("%d moeda(s) de R$ 0.50\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 0.5;
   }
   else
   {
      printf("0 nota(s) de R$ 0.50\n");
   }
   if (aux / 0.25)
   {
      aux = aux / 0.25;
      printf("%d moeda(s) de R$ 0.25\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 0.25;
   }
   else
   {
      printf("0 nota(s) de R$ 0.25\n");
   }
   if (aux / 0.1)
   {
      aux = aux / 0.1;
      printf("%d moeda(s) de R$ 0.10\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 0.1;
   }
   else
   {
      printf("0 nota(s) de R$ 0.10\n");
   }
   if (aux / 0.05)
   {
      aux = aux / 0.05;
      printf("%d moeda(s) de R$ 0.05\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 0.05;
   }
   else
   {
      printf("0 nota(s) de R$ 0.05\n");
   }
   if (aux / 0.01)
   {
      aux = aux / 0.01;
      printf("%d moeda(s) de R$ 0.01\n", (int)aux);
      aux = aux - (int)aux;
      aux = aux * 0.01;
   }
   else
   {
      printf("0 nota(s) de R$ 0.01\n");
   }
   printf("aux = %f\n", aux);

   return 0;
}

Output

aux = 576.729980
NOTAS:
5 nota(s) de R$ 100.00
1 nota(s) de R$ 50.00
1 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
1 nota(s) de R$ 5.00
0 nota(s) de R$ 2.00
MOEDAS:
1 moeda(s) de R$ 1.00
1 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
2 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
2 moeda(s) de R$ 0.01
aux = 0.009965
    
asked by anonymous 24.03.2018 / 19:31

0 answers