0

I am comparing two doubles like the following:

bool isbig = fabs(sin1 - sin(x)) > 0.000001;
if (isbig)
    printf("fabs=%f, max=%f", fabs(sin1-sin(x)), 0.000001);

Now, for some reason it prints in the output:

fabs=0.000001, max=0.000001

Why doesn't the comparison work? The print should only work if the left side is bigger than the right, but in the print we can see the sides are equal. Can somebody help? Thanks

INTODAN
  • 521
  • 1
  • 6
  • 14

1 Answers1

2

The %f format specifier to printf rounds to 6 decimal places by default, so it's entirely possible that fabs is something like 0.00000125 or 0.0000010064. Try something like %.16f to see more digits, and you should see fabs larger than max.

(And if you notice that max doesn't print as 0.00000100000000000000, go read Is floating point math broken?)

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82