# Is signed zero always available?

Steven D'Aprano steve at pearwood.info
Wed Jun 22 21:16:36 EDT 2016

```On Thu, 23 Jun 2016 08:50 am, Christopher Reimer wrote:

> When I took mathematics in college, the following was true:
>
> -1 * 0 = 0
>
> I would probably have gotten rapped on the knuckles by my instructors if I
> answered -0. Zero was zero. No plus or minus about that. No discussion of
> signed integers ever mentioned signed zero.
>
> Did I miss something in college?

Yes.

In IEEE-754 floating point maths, zero can represent two distinct concepts:
actual mathematical zero, and the result of a calculation which ought to be
small but non-zero, but due to the limitations of a fixed data size,
underflows to zero. Ideally, we would want *three* zeroes, representing
underflow to zero but positive, underflow to zero but negative, and true
mathematical zero.

Inconveniently, the standard floating point format leads to *two* different
representations of zero, one which is all zero bits, and one which is all
zero bits except for the sign bit. So the behaviour of IEEE-754 zeroes is a
mix of "treat them as the result of underflow" and "treat them as zero".

For example, the standard mandates that -0.0 must compare equal to 0.0, and
that in regular arithmetic there's no difference between the two: x+0.0 and
x-0.0 are the same. But that doesn't mean that they are always treated the
same. For instance, consider 1/x, where x is one of the zeroes. If we treat
them as "very small numbers which have underflowed", then 1/x should
be "very big numbers which will overflow", and IEEE-754 mandates two
special values to represent overflow: INF and -INF. So 1/0.0 may return
INF, and 1/-0.0 may return -INF.

(I say "may" rather than "will", because the standard allows that behaviour
to be configurable: you can specify whether to get ±INF or to signal divide
by zero. Unfortunately although most CPUs and FPUs support this behaviour,
support for it in compilers is mostly poor.)

But where signed zeroes really become useful is when dealing with branch
cuts for complex elementary functions:

http://people.freebsd.org/~das/kahan86branch.pdf

--
Steven

```

More information about the Python-list mailing list