Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uses for negative zero floating point value?

Tags:

Consider the following C++ code:

double someZero = 0; std::cout << 0 - someZero << '\n';   // prints 0 std::cout << -someZero << std::endl; // prints -0 

The question arises: what is negative zero good for, and should it be defensively avoided (i.e. use subtraction instead of smacking a minus onto a variable)?

like image 616
catfish_deluxe_call_me_cd Avatar asked Oct 30 '11 18:10

catfish_deluxe_call_me_cd


People also ask

Why is there a negative 0?

One may obtain negative zero as the result of certain computations, for instance as the result of arithmetic underflow on a negative number (other results may also be possible), or −1.0×0.0 , or simply as −0.0 .

Is there such thing as a negative zero?

There's no such thing as negative zero. For a binary integer, setting the sign bit to 1 and all other bits to zero, you get the smallest negative value for that integer size. (Assuming signed numbers.) Negative zero is actually used in mathematical analysis, especially in limit calculations.

Can negative numbers be represented in floating point?

Floating point numbers can be positive or negative. Ethereal provides two types of floating point numbers: regular floating point numbers, and double-precision floating point numbers.

Is there a difference between positive and negative zero?

Actually, zero is neither a negative or a positive number. The whole idea of positive and negative is defined in terms of zero. Negative numbers are numbers that are smaller than zero, and positive numbers are numbers that are bigger than zero.


1 Answers

From Wikipedia:

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

The first reference is "Branch Cuts for Complex Elementary Functions or Much Ado About Nothing's Sign Bit" by W. Kahan, that is available for download here.

One example from that paper is 1/(+0) vs 1/(-0). Here, the sign of zero makes a huge difference, since the first expression equals +inf and the second, -inf.

like image 100
NPE Avatar answered Oct 01 '22 12:10

NPE