0

I have a "strange" behaviour with a double:

#include <stdio.h>
int main()
{
    double var = 4724.735;
    double var1 = 472473.5;
    printf("var: %19.11f var1: %19.11f\n", var, var1);
    var *= 100.0;
    printf("var * 100.0: %19.11f\n", var);
   return 0;
 }

The result is:

var:    4724.73500000000 var1:  472473.50000000000
var * 100.0:  472473.49999999994

Why after the * 100.0 operation I have that result instead of 472473.50000000000?

With a smaller number, i.e. var = 724.735, the problem doesn't exist.

Are you sure is a duplicate? The Is floating point math broken? is related to javascript and here the problem is in the first decimal and only if I use an integer part of 4+ number. Thank you

Community
  • 1
  • 1
  • The most commonly used representation of C `double` is IEEE 754 64-bit binary floating point. That is also the number representation used in Javascript, and the issues in question are common to all uses of the IEEE standard, if not to all binary floating point. The problem does exist with some smaller numbers. The error was not in the first decimal. The result differed from what you expected by 0.00000000006. – Patricia Shanahan Mar 14 '17 at 11:15
  • Ok, thank you. I have to investigate more, with *10 or *1000 the result is right. I didn't expect such problem with a number of that type with only 4 number in the integer part and 3 decimals, my fault. – user7707645 Mar 14 '17 at 11:22
  • Same in SQL, FORTRAN, Java, C# etc. All have the same issue. – Ben Mar 14 '17 at 16:46
  • Interesting, thank you, maybe there are some other type to use for "decimal precision"? For C maybe I can find some library – user7707645 Mar 15 '17 at 08:11

0 Answers0