What is the difference between -0 and 0?

In C ++, for example, fmod(-2,2) returns -0 . The expression -0 == 0 true, but the bit is different. What is the purpose of having something like -0 , which should be 0 , but presented differently? Is -0 in the same way as 0 in any calculations?

+7
c ++ floating-point
source share
4 answers

No, +0 and -0 not used equally in every calculation. For example:

 3Β·(+0) = +0 +0/-3 = -0 

I suggest you read What Every Computer Scientist Should Know About David Goldberg's Floating-Point Arithmetic , which sheds light on why + 0 and -0 are needed in floating point arithmetic and to what extent they differ.

Examples of how +0 ad -0 differ (and why this might be useful when dealing with complex values) can be found in Kahan, W. 1987. Separation abbreviations for complex elementary functions, under "State of Art in Numerical Analysis" ( I could not find the pdf file for this article, you can find it in your local university library).

+13
source share

Signed Zero The Wikipedia page will answer most of these questions:

The signed zero is zero with the associated sign. In ordinary arithmetic, -0 = 0. However, when calculating a certain number of representations, the existence of two zeros allows, often denoted by -0 (negative zero) and +0 (positive zero). This occurs in some signed numeric representations for integers, and in most floating-point representations. The number 0 is usually encoded as +0, however, either +0 or -0 can be represented.

The IEEE 754 standard for floating point arithmetic (most computers are currently used and programming languages ​​that support floating point numbers) requires both +0 and -0. zeros can be considered as a variant of the extended line of real numbers, such that 1 / -0 = -∞ and 1 / + 0 = + ∞, division by zero is undefined for Β± 0 / Β± 0.

(...)

It is argued that the inclusion of zero in IEEE 754 makes it easier to achieve numerical accuracy in some critical problems, especially when calculating complex elementary functions.

+10
source share

IEEE Standard 754 allows both +0 and -0. The same mantissa, a different sign. They must be the same in the calculations.

+3
source share

I believe that the negative sign is caused by the implementation of fmod (mis?), When the bit-sign is processed explicitly and is tied to the result at the end of processing.

-3
source share

All Articles