3

I never understood why -0 treated separately than 0.

The interesting fact is that 0 is equal with -0

> 0 === -0
true

Then, the question is: why is -0 treated separately than 0 and +0?

Is there any case when the sign before 0 matters?

In mathematics +0 is a value just a little greater than 0. Also, -0 is a value just a little lower than 0. For example: n / Infinity would return +0 and n / -Infinity -0 (supposing that n is a real number greater than 0). This happens in mathematics.

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • I think the answer may be, because that's how the language says it should be treated. – Liam Apr 29 '14 at 15:07
  • 3
    It is defined in http://en.wikipedia.org/wiki/IEEE_754. The simple reason is that in binary representation in memory there is one bit defining the sign of the number. Simply by this fact it is possible for all represented numbers to be positive or negative (combinatorially). – Laszlo Korte Apr 29 '14 at 15:08
  • May be this Question and its answer give some idea [How do I check if a zero is positive or negative?](http://stackoverflow.com/questions/22409102/how-do-i-check-if-a-zero-is-positive-or-negative) Not sure but ;) – Grijesh Chauhan Apr 29 '14 at 15:08
  • I may be wrong but doesn't `-0` and `+0` parse it to a number? So it's basically the equivalent of `Number(-0)` and `Number(0)`? – Dom Apr 29 '14 at 15:09
  • If you add a arithmetic operator to any type in javascript it becomes a number. So if you add it to a number it stays a number. – A1rPun Apr 29 '14 at 15:10
  • 1
    To begin with there are not 3 zero : just two : +0 and -0. – GameAlchemist Apr 29 '14 at 15:15
  • it's essentially down to the fact that the binary 1000...0000 is the same as 0000...0000 if you're using signed integers (i.e one bit is used to show the sign) – Niall Paterson Apr 29 '14 at 15:16
  • @talemyn Also of this one: http://stackoverflow.com/q/7223717/1420197 – Ionică Bizău Apr 29 '14 at 15:16
  • @GameAlchemist I agree. Just 2 votes more are needed. :-) Anyway, some interesting answers I found. Thank you! – Ionică Bizău Apr 29 '14 at 15:29
  • If you really agree, then delete ! ;-) ;-) – GameAlchemist Apr 29 '14 at 15:30
  • @GameAlchemist *close* is not *delete*. Even a question is duplicate it doesn't mean that it should be deleted. More questions (even duplicated) helps Google to give you better results. – Ionică Bizău Apr 29 '14 at 15:36

1 Answers1

1

"Branch Cuts for Complex Elementary Functions or Much Ado About Nothing's Sign Bit " addresses the reasons for signed zeros. That kind of analysis informed IEEE-754 which is the basis for most modern instruction sets and programming languages' floating point number behavior.

In brief, many common numeric functions can be continuous with signed zeroes at places where they cannot be with an unsigned zero leading to fewer NaN values and fewer special cases. Division is one such function.


Then, the question is: why is -0 treated separately than 0 and +0?

Just to be clear, there are only two zero values. -0 and +0. The tokens (0) can be substituted for the tokens (+0) wherever they occur without changing semantics.


The interesting fact is that 0 is equal with -0

0 === -0
true

This behavior is mandated by IEEE-754.

To test whether two numeric values are the "same":

function same(x, y) {
  if (x === y) {
    if (x !== 0) {
      return true;  // Non-zero values.
    } else {
      return (1/x === 1/y);  // Test signed-ness of zeroes.
    }
  } else {
    return x !== x && y !== y;  // Treat NaNs the same
  }
}
Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245