Why does Gnu Octave have negative zeroes?
Asked Answered
K

3

24

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.

Keithakeithley answered 21/1, 2010 at 14:7 Comment(3)
FYI: Octave is in no way "based on" Matlab. Octave is modeled after Matlab and designed to solve the same problems, but it definitely isn't based on Matlab.Meliorism
"standard mathematics" and floating point arithmetic are two radically different concepts.Linkous
For an integer, a negative zero is an abomination. But even they were possible in ones-complement computers. I think CDC made ones-complement machines way back in the day.Dapsang
H
31

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.

Hammock answered 21/1, 2010 at 14:33 Comment(1)
Thanks for the Matlab catch. I added a note to the question.Keithakeithley
A
9

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.

Ambrotype answered 21/1, 2010 at 14:11 Comment(2)
Yes, in some maths it's important to make a distinction between -Inf and +Inf such as the log(0)=-InfCannon
Well, infinities always have a sign. That wasn't my point anyway.Ambrotype
T
7

Signed Zero

Signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0. The notation "−0" may be used informally to denote a negative number that has been rounded to zero. The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines.

Thriller answered 21/1, 2010 at 14:14 Comment(1)
Hmm, not sure what can be done, but some formatting was lost in the notation part, so now the first two pieces look identical.Keithakeithley

© 2022 - 2024 — McMap. All rights reserved.