Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does float have a negative zero? (-0f)

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 arithmetic operation I could do for example in C which result in a negative zero floating point value?

This question is inspired by another which called into question whether you can safely compare 0.0f using ==, and I wondered further if there is are other ways to represent zero which would cause float1 == 0.0f to break for seemingly perfectly equal values.

[Edit] Please, do not comment about the safety of comparing floats for equality! I am not trying to add to that overflowing bucket of duplicate questions.

like image 918
tenfour Avatar asked Feb 23 '11 19:02

tenfour


People also ask

What is a negative floating point?

Negative significands represent negative numbers. An exponent that says where the decimal (or binary) point is placed relative to the beginning of the significand. Negative exponents represent numbers that are very small (i.e. close to zero).

Can floats represent 0?

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).

Do floats have to be positive?

Floating point data types are always signed (can hold positive and negative values). When using floating point literals, always include at least one decimal place (even if the decimal is 0). This helps the compiler understand that the number is a floating point number and not an integer.

Can you be on negative 0$?

There's no such thing as negative zero. For a binary integer, setting the sign bit to 1 and all other bits to zero, you get the smallest negative value for that integer size. (Assuming signed numbers.) Negative zero is actually used in mathematical analysis, especially in limit calculations.


1 Answers

According to the standard, negative zero exists but it is equal to positive zero. For almost all purposes, the two behave the same way and many consider the existence of a negative to be an implementation detail. There are, however, some functions that behave quite differently, namely division and atan2:

#include <math.h> #include <stdio.h>  int main() {     double x = 0.0;     double y = -0.0;     printf("%.08f == %.08f: %d\n", x, y, x == y);     printf("%.08f == %.08f: %d\n", 1 / x, 1 / y, 1 / x == 1 / y);     printf("%.08f == %.08f: %d\n", atan2(x, y), atan2(y, y), atan2(x, y) == atan2(y, y)); } 

The result from this code is:

0.00000000 == -0.00000000: 1 1.#INF0000 == -1.#INF0000: 0 3.14159265 == -3.14159265: 0 

This would mean that code would correctly handle certain limits without a need for explicit handling. It's not certain that relying on this feature for values close to the limits is a good idea, since a simple calculation error can change the sign and make the value far from correct, but you can still take advantage of it if you avoid calculations that would change the sign.

like image 139
Rosh Oxymoron Avatar answered Oct 26 '22 20:10

Rosh Oxymoron