25

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)?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356

5 Answers5

24

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.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

In addition Signed Zero Good For :

The zeroes can be considered as a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞, division by zero is only undefined for ±0/±0.

Negatively 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 small negative number that has been rounded to zero. The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines

bilash.saha
  • 7,226
  • 2
  • 35
  • 40
  • 3
    I wonder if it would have been helpful to have three zeros: positive, negative, and unsigned, with subtraction of two negative numbers, or addition of two numbers of opposite sign and equal magnitude, yielding an unsigned zero? Division by a signed zero should yield infinity, but division by an unsigned zero should yield a NaN. – supercat Feb 22 '12 at 06:41
  • Interesting idea. In my opinion, if you introduce unsigned 0, unsigned division by 0 should yield unsigned infinity, and unsigned division of 0/0 should yield unsigned NaN. It would probably be easier to create new types that omit the sign bit, as opposed to mixing in new values that have a sign bit to disregard. Each "unsigned" value still has a sign bit that can flip, so you'd always have a corresponding "negative" copy that needs to represent the same value, compare as equal, etc. Good luck when it comes to -0.0 vs. 0.0 vs. unsigned 0.0, etc. – John P Aug 11 '22 at 01:38
  • Why did you repeat `x → 0−` twice? And why is there the sign after the 0? – Кое Кто Jul 23 '23 at 14:58
1

I'm making a measuring app, and the -0 is very useful for mixed numbers (such as separating into feet and inches).

Imagine that we have a variable "length" that we're trying to separate into "feet" and "inches".

(This is java code, but the same idea is true for C++).

feet = Math.signum(length) * Math.floor(Math.abs(length / 12));
// could also do feet = length>0 ? Math.floor(length / 12) : Math.ceil(length / 12)
inches = Math.abs(length) % 12;

If the length is between -1 feet and 0 feet, we'd want it to say -0 for the feet so we know it's negative.

J F Fitch
  • 116
  • 1
  • 3
  • If the length is 0 feet, why would `signum(0)*floor(abs(0))` be -0? That aside, you might be interested in "truncate", it's like floor for positives and ceil for negatives. If you use `truncate(x/12)` and `x/12-truncate(x/12)`, you preserve the sign of x but get integer division and modulo remainder of the magnitude. You also know for certain that `-1 < x-truncate(x) < 1`. – John P Aug 11 '22 at 01:57
1

There are only two real use-cases that I can see:

  1. You want to show that a value is negative but very very small (perhaps infinitessimal), i.e. too small to represent as a float or double.
  2. You are working with math that only allows negatives, but still want to display zero. There are a few cases in physics, complex numbers and number theory where this can be useful.

For the mostpart, it's not useful and should be avoided.

You may also want to take a look at this question: Is there a negative zero? and the IEEE 754 spec for floating point.

Community
  • 1
  • 1
Polynomial
  • 27,674
  • 12
  • 80
  • 107
0

Negative zero has for example some use when handling complex numbers...

In everyday use one should mostly avoid the negative zero.

Some links with information regarding background/uses/pitfalls of "negative zero":

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144