1

so i am having this strange problem here and I don't know what to do.

so in the following I am posting an excerpt of my code:

printf("%lf , %lf \n", cGrid_Y,sideLength);

printf("%lf <= %lf\n", Point_Y, cGrid_Y+sideLength);

bool x = (Point_Y <= (sideLength + cGrid_Y) );
printf("%s \n", x ? "true" : "false");

cGrid_Y and sideLength are doubles. And I am getting this output:

-12.800000 , 12.800000 
0.000000 <= -0.000000
false 

So my question is, why I am not getting a true ?

Chopin
  • 33
  • 2
  • 3
    Try printing with `%g`. – Keith Thompson Jan 01 '18 at 20:41
  • 2
    Possible duplicate of [strange output in comparison of float with float literal](https://stackoverflow.com/questions/1839422/strange-output-in-comparison-of-float-with-float-literal) – Igal S. Jan 01 '18 at 20:41
  • 2
    Try printing the value this way: printf("%le <= %le\n", Point_Y, cGrid_Y+sideLength); You may not be seeing a very small value. – EddingtonsMonkey Jan 01 '18 at 20:44
  • Your question was put on hold because you didn't provide enough information for us to be sure what the problem is. We don't know the values of `cGrid_Y` and `sideLength` or how they were set. Read this: [mcve]. – Keith Thompson Jan 01 '18 at 21:33

3 Answers3

6

This is not a problem with negative zeros. 0.0 <= -0.0 is true. The problem is that your values are not actually zero or negative zero but some very small value that's being rounded to 0 for presentation when you ask printf to show it rounded to 6 decimal places. Either print with %e or %g (which will use exponential notation to show a better approximation) or %.1100f which is sufficient precision to show the exact value of any double.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Yes i have done this an they are actually not equal to zero. I have now rounded them to their e^-10 decimal space. Now it is working. Thank you – Chopin Jan 01 '18 at 21:14
2

So my question is, why I am not getting a true ?

Are you sure that Point_Y == 0.0? Same for the sum of sideLength + cGrid_Y? Check your assumptions:

printf("Point_Y is zero: %d\n", Point_Y == 0.0);
printf("`sideLength + cGrid_Y` is zero: %d\n", (sideLength + cGrid_Y) == 0.0);

The reason you got confused is because by default printf's floats/doubles are't printed to full precision.

#include <stdio.h>

int main()
{
  double x = 0.0000001;
  printf("x: %f\n", x);
}

outputs: x: 0.000000

Pavel P
  • 15,789
  • 11
  • 79
  • 128
0

@edit I wrote a stupid mistaken thing: I thought that negative zero doesn't equal positive zero, whereas after reading it all properly -0 equals +0.

The source of your problem: you are trying to test whether two doubles are equal, and this is a terrible idea, due to the imprecision of floating point representation. Note the common example: 0.1 + 0.2 does NOT equal 0.3:

Is floating point math broken?

https://www.quora.com/Why-is-0-1+0-2-not-equal-to-0-3-in-most-programming-languages

You cannot represent 12.8 in binary without losing a part of the imformation, due to the fundamental nature of how this number is stored in the computer's memory.

The same way you can't represent 1/7 in our "normal" decimal representation of numbers with a finite amount of digits.

tehftw
  • 149
  • 1
  • 6