0

why is there a negative 0.000?

and why is it not equal to 0.000?

  • 1
    Read about [signed zero](https://en.wikipedia.org/wiki/Signed_zero). For the second question, they should be equal. Please show some code where you think they are exhibiting unequal behaviour. – kaylum Oct 17 '15 at 05:22
  • Re the second question. See http://ideone.com/w7ozc2. – R Sahu Oct 17 '15 at 05:23
  • I can see the IEEE-754 rationale but it's still a bit of a hack. Separate ±1/∞ representations for underflow would seem more natural to me but presumably they would not sufficiently affect the results of actual numerical methods to warrant the silicon space. – doynax Oct 17 '15 at 05:35

2 Answers2

2

The concept of positive and negative zero is a feature of IEEE 754 floating point formats. There is no requirement that a C implementation will use IEEE floating point formats although, in practice, it is reasonably common.

Even with C implementations that use IEEE floating point formats, positive and negative zero will always compare equal, when using numerical comparisons (such as the == operator). The only way to detect or work with positive and negative zeros on such implementations is to use functions like copysign() and signbit() (declared in <math.h>).

Peter
  • 35,646
  • 4
  • 32
  • 74
0

why is there a negative 0.000?

This may help a bit
What operations and functions on +0.0 and -0.0 give different arithmetic results?

why is it (negative 0.000) not equal to 0.000?

This is a common mis-conclusion. Reveiw the following:

double a = 1e-10;
double b = -2e-20;
printf("a:%f b:%f\n", a, b);
printf("a==b? %d\n", a == b);
printf("a:%e b:%e\n", a, b);

double z = 0.0;
double nz = -0.0;
printf("n:%f nz:%f\n", z, nz);
printf("n==nz? %d\n", z == nz);

Output

a:0.000000 b:-0.000000   // a and b appear to be 0.0 and -0.0
a==b? 0                  // not same value
a:1.000000e-10 b:-2.000000e-20

n:0.000000 nz:-0.000000  // n and nz appear to be 0.0 and -0.0
n==nz? 1                 // same value
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256