java – What is the difference between negative zero and positive zero?

Question:

Floating point values ​​are orderable. In order from smallest to largest are negative infinity, negative terminal nonzero values, negative and positive zeros , positive terminal nonzero values, and positive infinity.

What is the difference between negative zero and positive zero? It is clear that it is familiar, but zero is zero. What is the need for such a use of zero?

Answer:

Floating point values, according to the standard , are encoded as follows:

знак (1 бит) | экспонента (8 бит) | дробная часть (23 бита)

If we want to represent 0 in this format, then we will “fill in” the exponent and the fractional part with zeros. But there is still a sign! So it turns out that there are two zeros – one negative (leading bit is 1) and one positive (leading bit is 0).

In arithmetic operations, they behave the same (except for division). The presence of plus and minus is due to the specifics of storage.

Scroll to Top