I read 576.73 but it is displayed as 576.729980!

Asked

Viewed 72 times

0

Problem

The source code is only to return the amount of banknotes and coins based on an input value.
However, I am having trouble manipulating the value of the variable aux. In the demonstrated example, the value of input is 576.73. But, as you can see, the variable aux has loss and this is making my program work 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
  • Using floating point mathematics, the sum is neither commutative nor associative. The following sums can present different results: (a+b)+c and a+(b+c)

  • does not make sense on this site to put tags between brackets in the title asking for help. Nor does language tags between brackets in the title. You have already labeled this question as C, the way you created the title (and subsequently reissued it to) does not contain any relevant information to address your doubt. The call for help for obvious reasons, the language because this problem is already registered as C.

  • Read the questions (and their answers) that marked as duplicates of yours. There is an explanation for your problem in them. If any doubt remains after the uplifting reading, you will soon have enough reputation to ask directly in the answer of others the specific point of your doubt. If you felt that none of the questions answered you satisfactorily, make it clear in the body of the text of your question that the duplicates are not enough to resolve your doubt, explaining in more depth what is the core of your question that has not yet been properly addressed.

  • Particularly I realized that your question has already been answered in one of these questions. Whoever marked this question as duplicate also perceived it so. If you want a less informal approach while you still don’t have the reputation to comment freely, you can put a comment here marking me that I will try to help you. But before you try to contact me, lovingly absorb the questions and related answers in duplicates

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.