0

I know that JavaScript has both a normal zero 0 (known as a positive zero +0) and a negative zero -0, however I have never come across a situation where I had to use -0.

There are some existing posts on stack overflow about how positive and negative zeros are similar/different, but none of them explain real life use-cases/examples of it.

Aaditya Sharma
  • 3,330
  • 3
  • 24
  • 36
  • Why is this queation relevant?? – Jonas Wilms Sep 15 '16 at 12:40
  • 1
    In my opinion it is more mathematical representation. + and - are signs that basically helpful to add some directionality sense to the value. For instance, if you are calculating `2/0 = Infinity` , `2/-0 = - infinity` . So, now infinity has a context in your study or algorithm or some calculations. The `- infinity` represents the value to be very low. While `infinity` represents the value to be very high. – user5249203 Sep 15 '16 at 12:41
  • http://stackoverflow.com/questions/7223359/are-0-and-0-the-same – epascarello Sep 15 '16 at 13:11

2 Answers2

2

Assume we're studying the function y = 1/x and we'd like to know how it behaves when x is small. Let's take x=1, x=0.1, x=0.01 and calculate the func:

x = 1;
while(x) {
    x /= 10;
    document.write(x + ' ' + 1/x + '<br>');
}

As you can see, it approaches towards positive infinity. 1/x is equal to Infinity because at some point x gets so small that it's indistinguishable from 0, and 1/0 = Infinity. Note that this is the "positive" Infinity, that is, "a very big number".

Now, let's start with -1 instead of x=1:

x = -1;
while(x) {
    x /= 10;
    document.write(x + ' ' + 1/x + '<br>');
}

The answer is now -Infinity, that is, the function approaches towards the negative Infinity, "a very small number". Of course, this is also correct, but how did the computer get that? We just learned that 1/0 = (positive) Infinity? The secret is that the zero in the last snippet is actually negative, so x on the last iteration is -0 and not just 0, and 1/-0 gives -Infinity. Without the signed zero, the last snippet would give an incorrect result.

Hope that explains it a bit.

georg
  • 211,518
  • 52
  • 313
  • 390
0

There are scientific reasons for including the sign, which you can read in more depth here on wikipedia.

From the Wikipedia article on Signed Zero:

Informally, one may use the notation "−0" for a negative value that was rounded to zero. This notation may be useful when a negative sign is significant; for example, when tabulating Celsius temperatures, where a negative sign means below freezing.

In statistical mechanics, one sometimes uses negative temperatures to describe systems with population inversion, which can be considered to have a temperature greater than positive infinity, because the coefficient of energy in the population distribution function is −1/Temperature. In this context, a temperature of −0 is a (theoretical) temperature larger than any other negative temperature, corresponding to the (theoretical) maximum conceivable extent of population inversion, the opposite extreme to +0.[5]"