While working on a program that I have written using C, I noticed an unusual thing. I have defined a constant a=0.0
of type double. Then, when I print a
the return value is 0.0000
as usual, but when I print -a
the return value is -0.0000
. I don't think that it is normal because a negative sign with zero is meaningless. Can someone please point out if I am doing something wrong?
Asked
Active
Viewed 95 times
0

Fabio says Reinstate Monica
- 5,271
- 9
- 40
- 61

Richard
- 155
- 6
-
5Show your exact code. – Eugene Sh. May 31 '23 at 13:51
-
4"I don't think that it is normal because a negative sign with zero is meaningless." It's not. A common question in mathematics is to see what the [limit](https://www.mathsisfun.com/calculus/limits.html) behaviour of a function is as it approaches some x value. Distinguishing between `0` and `-0` can indicate whether you hit zero "coming from the left" (your function was negative, but increased to zero) or "from the right" (your function starts near zero, and grows to be more positive). See [Signed zero](https://en.wikipedia.org/wiki/Signed_zero) – Alexander May 31 '23 at 13:54
-
2Also if you `printf("%f", -DBL_MIN / 2);` it correctly reports `-0.000000`. The value *isn't* zero but this is the best representation. – Weather Vane May 31 '23 at 15:09