Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do floating-point numbers have signed zeros?

Why do doubles have -0 as well as +0? What is the background and significance?

like image 463
Ken Russell Avatar asked Nov 24 '12 18:11

Ken Russell


People also ask

Can you represent 0 in floating-point?

The number 0 is usually encoded as +0, but 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.

Are floating-point numbers signed?

Floating-point representation is similar in concept to scientific notation. Logically, a floating-point number consists of: A signed (meaning positive or negative) digit string of a given length in a given base (or radix). This digit string is referred to as the significand, mantissa, or coefficient.

What does float a 0.0 0 return mean?

"-0.0" is produced when a floating-point operation results in a negative floating-point number so close to 0 that it cannot be represented normally.

How are floating-point numbers represented?

In computers, floating-point numbers are represented in scientific notation of fraction ( F ) and exponent ( E ) with a radix of 2, in the form of F×2^E . Both E and F can be positive as well as negative.


2 Answers

-0 is (generally) treated as 0 *******. It can result when a negative floating-point number is so close to zero that it can be considered 0 (to be clear, I'm referring to arithmetic underflow, and the results of the following computations are interpreted as being exactly ±0, not just really small numbers). e.g.

System.out.println(-1 / Float.POSITIVE_INFINITY); 
 -0.0 

If we consider the same case with a positive number, we will receive our good old 0:

System.out.println(1 / Float.POSITIVE_INFINITY); 
 0.0 

******* Here's a case where using -0.0 results in something different than when using 0.0:

System.out.println(1 / 0.0); System.out.println(1 / -0.0); 
 Infinity -Infinity 

This makes sense if we consider the function 1 / x. As x approaches 0 from the +-side, we should get positive infinity, but as it approaches from the --side, we should get negative infinity. The graph of the function should make this clear:

(source)

In math-terms:

enter image description here

enter image description here

This illustrates one significant difference between 0 and -0 in the computational sense.


Here are some relevant resources, some of which have been brought up already. I've included them for the sake of completeness:

  • Wikipedia article on signed zero
  • "What Every Computer Scientist Should Know About Floating-Point Arithmetic" (See Signed Zero section)
  • (PDF) "Much Ado About Nothing's Sign Bit" - an interesting paper by W. Kahan.
like image 146
13 revs Avatar answered Sep 18 '22 19:09

13 revs


From Wikipedia

Signed zero is zero with an associated sign. In ordinary arithmetic, −0 = +0 = 0. In computing, existes the concept of existence of two zeros in some numbers representations, usually denoted by −0 and '+0', representing negative zero and +0 positive zero, respectively (source).

This occurs in the sign and magnitude and ones' complement signed number representations for integers, and in most floating point number representations. The number 0 is usually encoded as +0, but can be represented by either +0 or −0.

According to the IEEE 754 standard, negative zero and positive zero should compare as equal with the usual (numerical) comparison operators, like the == operators of C and Java. (source).

When you have a floating-point operation that produces a result that is a negative floating-point number close to zero, but that can not be represented (by the computer) it produces a "-0.0". For example -5.0 / Float.POSITIVE_INFINITY -> -0.0.

This distinction between -0.0 and +0.0 gives the end-user more information than merely displaying a final result of 0. Naturally, such a concept is really only useful in systems with a finite numerical representation limitation such as those of computers. In mathematics, one can represent any number, regardless of how close it is to zero.

−0 and +0 are the result of mathematical operations performed by computers that cause underflows, similar to the −00 or +00 that result from operations that cause an overflow. For the operations that cause mathematical indetermination, the result is NaN (e.g., 0/0).

What's the difference between -0.0 and 0.0?

In reality, both represent 0. Furthermore, (-0.0 == 0.0) returns true. Nevertheless:

  1. 1/-0.0 produces -Infinity while 1/0.0 produces Infinity.

  2. 3 * (+0) = +0 and +0/-3 = -0. The sign rules applies, when performing multiplications or division over a signed zero.

Mandatory reading "What Every Computer Scientist Should Know About Floating-Point Arithmetic".

like image 38
dreamcrash Avatar answered Sep 18 '22 19:09

dreamcrash