7

In a recent post on http://wtfjs.com/. An author writes following without explanation which happens to be true.

0 === -0 //returns true

My understanding about === operator is it returns true if operands point to same object.

Also, - operator returns a reference to negative value of operand. With this rule, 0 and -0 should not be the same.

So, why is 0 === -0 ?

3 Answers 3

7

=== does not always mean point to the same object. It does on objects, but on value types, it compares the value. Hence how this works:

var x = 0;
var y = 0;
var isTrue = (x === y);
document.write(isTrue); // true

JavaScript used IEEE floating point standard where 0 and -0 are two different numbers, however, the ECMAScript standard states the parser must interpret 0 and -0 as the same:

§5.2 (page 12)

Mathematical operations such as addition, subtraction, negation, multiplication, division, and the mathematical functions defined later in this clause should always be understood as computing exact mathematical results on mathematical real numbers, which do not include infinities and do not include a negative zero that is distinguished from positive zero. Algorithms in this standard that model floating-point arithmetic include explicit steps, where necessary, to handle infinities and signed zero and to perform rounding. If a mathematical operation or function is applied to a floating-point number, it should be understood as being applied to the exact mathematical value represented by that floating-point number; such a floating-point number must be finite, and if it is +0 or -0 then the corresponding mathematical value is simply 0.

Sign up to request clarification or add additional context in comments.

7 Comments

so, is it a special case implemented in JavaScript or negative numbers are evaluated using 2's complement?
@hrishikeshp19 IEEE floating point standard is ones complement with the exception of this.
It's not just the parser. a === -a where a is 0 returns true.
@ColeJohnson Nice excerpt (by itself). I didn't realize it was so blunt about the handling of -0.
@pst Neither did I until I tried searching the standard PDF for negative to find the block
|
6

In fact, 0 and -0 are not the same even at the bit level. However, there is a special case implemented for +/-0 so they compare as equal.

The === operator compares by value when applied to primitive numbers.

4 Comments

It ones complement they aren't the same, but in twos they are.
@ColeJohnson I forgot about two's complement for a bit. Javascript uses IEEE 64-bit floating point for numbers, which is one's complement.
so it seems special case handled in JavaScript.
That's right. I don't think the special case is specific to Javascript either.
3

Primitive numbers are not objects. You're doing a value comparison of the numbers, not an identity comparison of objects.

positive zero is equal to negative zero.

This is from the comparison algorithm for the === operator

If Type(x) is Number, then

  • If x is NaN, return false.

  • If y is NaN, return false.

  • If x is the same Number value as y, return true.

  • If x is +0 and y is −0, return true.

  • If x is −0 and y is +0, return true.

  • Return false.

1 Comment

+1. Because -0 is actually a number in JavaScript (due to all numbers really being IEEE-754 floats, which can represent negative zero). It is just defined to be equal to 0 so people don't go crazy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.