6

While experimenting with JavaScript. I was testing around with some odd little code snippets, here are a few of my findings (to help understand how I came upon -0),

While doing +[] in console, this returns 0. I'm not sure why, but it does. So, this implies the positive of a array is 0..

After doing so, I got curious and tried the following:

console.log(-[]);

And this returns -0...

What is the point of -0? 0 and -0 both hold no value, so the negative is really unnecessary... Or is it? Perhaps JavaScript has a purpose for -0?

Adding onto that. I cannot find any other way to reproduce -0 other than using -0 itself, or using -[]...


Some other weird findings, furthing my question of -0

(-0) + (-0) = 0
(-0) - (-0) = -0
(-0) * (-0) = 0
(-0) / (-0) = NaN // of course
  • `-0 < 0` -> `false`; `-0 == 0` -> `true`; `-0 === 0` -> `true` – i_am_jorf Nov 22 '14 at 05:22
  • it's a legacy thing, i wouldn't worry too much about it. – dandavis Nov 22 '14 at 05:23
  • Also found this one: http://stackoverflow.com/questions/7223717/differentiating-0-and-0 – Krease Nov 22 '14 at 05:24
  • +[] ==0 because "+" forces a number, and wanting a number from an array goes through the array's toString methods, which is a blank string, and the number equiv of a blank string is 0. – dandavis Nov 22 '14 at 05:24
  • 1
    I'm asking `why does it exists`, not the difference. –  Nov 22 '14 at 05:25
  • 2
    http://en.wikipedia.org/wiki/Signed_zero#Scientific_uses – Felix Kling Nov 22 '14 at 05:34
  • @FelixKling: Thanks for that, now that does seem useful. Math.round(-0.3) //outputs -0 Math.round(0.3) //outputs 0 Useful to know from which side I rounded to 0. – Aravind Nov 22 '14 at 05:38
  • The irony of my username at this point. Xero learns Zeros –  Nov 22 '14 at 05:39
  • 4
    when God and Brendan created JS in 6 days, they split up the workload, and Brendan, human to a fault, nibbled on the forbidden fruit of C knowledge. We mortals forever suffer because of his original sin. – dandavis Nov 22 '14 at 05:47

2 Answers2

3

JavaScript uses IEEE 754 standard to represent numbers. From Wikipedia:

Signed zero is zero with an associated sign. In ordinary arithmetic, −0 = +0 = 0. However, in computing, some number representations allow for the existence of two zeros, often denoted by −0 (negative zero) and +0 (positive zero). This occurs in some signed number representations for integers, and in most floating point number representations. The number 0 is usually encoded as +0, but can be represented by either +0 or −0.

The IEEE 754 standard for floating point arithmetic (presently used by most computers and programming languages that support floating point numbers) requires both +0 and −0. 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 and ±∞/±∞.

From this Post

Community
  • 1
  • 1
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
2

It's to help determine what happens when you divide by it.

If you divide by +0, you get +Infinity. If you divide by -0, you get -Infinity. (For nonzero numerators, of course).

user513951
  • 12,445
  • 7
  • 65
  • 82
  • I'm curious, why is it present? What's a use case for it? – Aravind Nov 22 '14 at 05:29
  • I am asking the use case for -0. Where does it come up? How can I use it in practice? – Aravind Nov 22 '14 at 05:32
  • @Aravind, as said in my question, the only placed I could find it come up is when using it directly (`-0`), or the negative of an array (`-[]`). I'm still wondering what it's for though, why does it not all round out to `+0` or `0`? –  Nov 22 '14 at 05:33
  • @JesseSielaff: Sure, this is a use case. The use case of rounding to 0 from either side seems more compelling of a use case for signed zero. – Aravind Nov 22 '14 at 05:42