0

I was reading a book called Speaking JavaScript by Axel Rauschmayer. Here, I came to know that -0 value exists in JavaScript.

So, below is a valid expression:

var num = -0;

After exploring further, I came to know that it's value is similar to 0.

var num1 = -0;
var num2 = 0;
num1===num2//true;

As per Mathematics, 0 is a non-negative number. So, basically JavaScript is violating mathematics principle.

I am curious to know the reason behind doing so. Does it has any crucial significance? Note: all the above observation were made on Google Chrome. Thanks.

Aman Jain
  • 655
  • 5
  • 17
  • 1
    I don't see any point that says, you cannot assign a `-0` negative 0. This is same as doing `a = a * 1` which makes no sense. – Praveen Kumar Purushothaman Apr 19 '17 at 09:28
  • 3
    https://en.wikipedia.org/wiki/Signed_zero – deceze Apr 19 '17 at 09:29
  • But as par maths, -0 does not makes any sense. 0 is a non negative number. – Aman Jain Apr 19 '17 at 09:29
  • 1
    Even you can have `var num = +0` and get `-0 === +0 // true` – Om Sao Apr 19 '17 at 09:29
  • 1
    @Spidi It's not "non-negative", it's neither positive *nor* negative. – deceze Apr 19 '17 at 09:32
  • - is a operator and not really math per se. – NeutronCode Apr 19 '17 at 09:29
  • Yes, that's the point. So why specifying -0? – Aman Jain Apr 19 '17 at 09:33
  • 3
    You *did* see the duplicate and the aforelinked Wikipedia article that answers that in-depth…? – deceze Apr 19 '17 at 09:34
  • This is _not_ violating any principle in maths. Zero is zero, no matter what sign you try to attach to it. Think of it like: (+1)*0 == (-1)*0. There's nothing fancy or strange here. – garglblarg Apr 19 '17 at 09:34
  • @deceze Hence "non-negative", not "positive"; mathematically, it's "non-negative and non-positive". – IMSoP Apr 19 '17 at 09:34
  • 2
    @Spidi'sWeb: It's part of the IEEE-754 standard, which is what covers JavaScript's numbers; see the answers to the linked questions. Specifically, JavaScript's numbers are IEEE-754 double-precision binary floating point, which have both 0 and -0 and do several things that aren't quite right according to pure mathematics, such as `0.1 + 0.2 = 0.30000000000000004`. – T.J. Crowder Apr 19 '17 at 09:35
  • -0 operates into the value 0. Minus is a function creating a result. There is no value -0 as there is no value +0. – NeutronCode Apr 19 '17 at 09:35
  • @NeutronCode: *"There is no value -0"* If you're talking about JavaScript, yes, there is: https://en.wikipedia.org/wiki/Signed_zero – T.J. Crowder Apr 19 '17 at 09:36
  • @T.J.Crowder Makes sense. I'll look to the brief of these standards. :) Thanks. – Aman Jain Apr 19 '17 at 09:37
  • 1
    Seriously, people, **read the linked answers and explanations**. Yes, there really is a negative zero, in pretty much every modern programming language; yes, it is defined as "equal to" positive zero, but is represented differently to allow a few special operations. No, this doesn't make sense in standard arithmetic, it's just a hack for digital representation. – IMSoP Apr 19 '17 at 09:38
  • @IMSoP What are those special operations? Can you please elaborate? – Aman Jain Apr 19 '17 at 09:41
  • 1
    @Spidi'sWeb **Read the links.** This is a Q&A site, not a discussion forum. You asked a Question, and it has been marked as already having an answer elsewhere. So the Answers to your Question are here: http://stackoverflow.com/questions/7223359/are-0-and-0-the-same – IMSoP Apr 19 '17 at 09:41
  • Because programming is not the same thing as mathematics – Samurai Jack Apr 19 '17 at 09:54

0 Answers0