Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Gnu Octave have negative zeroes?

Tags:

This is an odd one I'm puzzled about. I recently noticed at the Gnu Octave prompt, it's possible to enter in negative zeroes, like so:

octave:2> abomination = -0 

And it remembers it, too:

octave:3> abomination abomination = -0 

In the interest of sanity, negative zero does equal regular zero. But I also noticed that the sign has some other effects. Like these:

octave:6> 4 * 0 ans = 0 octave:7> 4 * -0 ans = -0 octave:8> 4 / 0 warning: division by zero ans = Inf octave:9> 4 / -0 warning: division by zero ans = -Inf 

As one can see, the sign is preserved through certain operations. But my question is why. This seems like a radical departure from standard mathematics, where zero is essentially without sign. Are there some attractive mathematical properties for having this? Does this matter in certain fields of mathematics?

FYI: Matlab, which octave is modeled after, does not have negative zeros. Any attempts to use them are treated as regular zeros.

EDIT: Matlab does have negative zeros, but they are not displayed in the default output.

like image 578
BigBeagle Avatar asked Jan 21 '10 14:01

BigBeagle


2 Answers

Signed zero are part of the IEEE-754 formats, and their semantics are completely specified by those formats. They turn out to be quite useful, especially when dealing with complex branch cuts and transformations of the complex plane (see many of W. Kahan's writings on the subject for more details, such as the classic "Branch Cuts for Complex Elementary Functions, or Much Ado about Nothing's Sign Bit").

Short version: negative zero is often a good thing to have in numerical calculations, and programs that try to protect users from encountering it are often doing them a disservice. FWIW, MATLAB does seem to use negative zero as well, but since it prints numbers using the host's printf routine, they display the same as positive zero on Windows.

See this discussion on the MATLAB forums for more details on signed zero in MATLAB.

like image 118
Stephen Canon Avatar answered Sep 19 '22 13:09

Stephen Canon


IEEE-754 floating point numbers have this property too. It might come in handy for limits and infinities. For example, the limit of 1/x with x → +∞ is 0, but the function approaches from the positive side of the axis, with x → −∞ the function approaches from the negative side so one might give the limit as −0, in that case.

like image 34
Joey Avatar answered Sep 19 '22 13:09

Joey