views:

321

answers:

4

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?

+3  A: 

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

alxx
Except for when they're different; `1/x`, for example.
Stephen Canon
Okay, different. I don't divide by zero that often, so forgive me.
alxx
+10  A: 

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.

Kornel Kisielewicz
A: 

i believe the negative sign is caused by fmod (mis?)implementation, where the sign bit is handled explicitly and attached back to the result at the end of processing.

YeenFei
i love downvotes without comments
YeenFei
-1 wrong answer
Tomas
@Tomas, constructive reply.
YeenFei
@YeenFei - I'm sorrry but there is not much to add. The negative sign is not caused by fmod (mis-)implementation. Read the other answers for an explanation.
Tomas
+13  A: 

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

Giuseppe Cardone
The link explains it in a good way!
Danvil