Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between -0 and 0?

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

like image 933
Danvil Avatar asked Sep 14 '10 09:09

Danvil


3 Answers

No, +0 and -0 are not used in the same way in every computation. For example:

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

I suggest you to read What Every Computer Scientist Should Know About Floating-point arithmetic by David Goldberg, that sheds a light on why +0 and -0 are needed in floating point arithmetic and in which way they differ.

Examples on how +0 ad -0 differ (and why that can be useful when dealing with complex values) can be found in Kahan, W. 1987. Branch Cuts for Complex Elementary Functions, in "The State of the Art in Numerical Analysis" (I wasn't able to find a pdf of this article, you may find one at your local university library).

like image 118
Giuseppe Cardone Avatar answered Oct 25 '22 00:10

Giuseppe Cardone


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

Signed zero is zero with an associated sign. In ordinary arithmetic, −0 = 0. However, in computing, some number representations allow for the existence of two zeros, often denoted by −0 (negative zero) and +0 (positive zero). This occurs in some signed number representations for integers, and in most floating point number representations. The number 0 is usually encoded as +0, however it can be represented by either +0 or −0.

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

(...)

It is claimed that the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems, in particular when computing with complex elementary functions.

like image 26
Kornel Kisielewicz Avatar answered Oct 25 '22 00:10

Kornel Kisielewicz


IEEE Standard 754 allows both +0 and -0. Same mantissa, different sign. They should be the same in computations.

like image 44
alxx Avatar answered Oct 25 '22 01:10

alxx