IEEE floating point numbers have a bit assigned to indicate the sign, which means you can technically have different binary representations of zero (+0 and -0). Is there an
Yes, float
s have negative zero just like other IEEE floating point types such as double
(on systems with IEEE floating point). There is an example here in Octave of how to create them; the same operations work in C. The ==
operator treats +0 and -0 the same, though, and so negative zeros do not break that type of comparison.
Yes, float does have a negative zero, but no, you don't have to worry about this when comparing floating-point values.
Floating-point arithmetic is defined to work correctly on special cases.
-lm has the signbit() function available to indicate if a value is negative (including -0)
There are a couple of simple arithmetic operations that result in a negative zero answer (at least on the i386/x64/ARMv7/ARMv8 systems I tested it on) :
These caught me by surprise when I was writing an optimiser to simplify arithmetic expressions. Optimising "a = b * 0" to "a = 0" will result in the wrong answer (+0) if b happens to be negative (correct answer is -0).