Do you know there are two valid zero representations in JavaScript?

posZero = +0; negZero = -0;

In pure mathematics, zero means nothing and its sign doesn’t matter. +0 = -0 = 0. Computers can’t represent value well enough and mostly use the IEEE 754 standard.

### Most languages have two zeros!

The IEEE 754 standard for floating point numbers allows for signed zeros, thus it is possible to have both -0 and +0. Correspondingly, 1 / +0 = +∞ while 1 / -0 = -∞ and these are values at opposite ends of the number line.

- They can be viewed as vectors with zero magnitude pointing in opposite directions.
- In the mathematical field of limits, negative and positive zeros show how zero was reached.

These two zeros can lead to issues as shown with the disparate ∞ results.

#### Why two zeros occur in IEEE 754

There is a bit representing the sign of each numeric value independent of its magnitude. Consequently if the magnitude of a number goes to zero without its sign changing then it becomes a -0.

So why does this matter? Well, JavaScript implements the IEEE 754 standard and this post goes into some of the details.

Keep in mind, the default zero value in JavaScript (and most languages) is actually the signed zero (+0).

### The zeros in JavaScript

#### 1. Representation

let a = -0; a; // -0 let b = +0; b; // 0

#### 2. Creation

All mathematical operations give a signed zero result (+0 or -0) that depends on the operand values.

The only exception to this rule involves addition and subtraction involving +0 and -0.

- Adding two -0 values will always be -0
- Subtracting a 0 from -0 will also be -0

Any other combination of zero values gives a +0. Another thing to note is that negative zeros cannot be created as a result of addition or subtraction of non-zero operands. Thus -3 + 3 = 3 – 3 = +0.

The code below shows some more examples.

// Addition and Subtraction 3 - 3 // 0 -3 + 3 // 0 // Addition of zero values -0 + -0; // -0 -0 - 0; // -0 0 - 0; // 0 0 + -0; // 0 // Multiplication 3 * 0 // 0 3 * -0 // -0 // Division 3 / Infinity // 0 -3 / Infinity // -0 // Modulus 6 % 2 // 0 -6 % 2 // -0

#### 3. The issue with zero strings

There is a minor niggle with stringifying -0. Calling *toString* will always give the result “0”. On the flip side, *parseInt* and *parseFloat* parse negative zero values.

Consequently, there is a loss of information in the stringify -> *parseInt* transformation. For example, if you convert values to strings (for example, via *JSON.stringify), *POST to some server and then retrieve those strings later.

let a = '-0'; a.toString(); // '0' parseInt('-0', 10); // -0 parseFloat('-0', 10); // -0

#### 4. Differentiating between +0 and -0

How would you tell one zero value apart from the other? Let’s try comparison.

-0 === 0; // true -0..toString(); // '0' 0..toString(); // '0' -0 < 0; // false 0 < -0; // false

*0..toString()* is valid JavaScript. Read this to know why.

ES2015’s *Object.is* method works

Object.is(0, -0); //false

The ES2015’s *Math.sign* method for checking the sign of a number is not of too much help since it returns 0 and -0 for +0 and -0 respectively.

Since ES5 has no such helper we can use the difference in behaviour of +0 and -0 to write a helper.

function isNegativeZero(value) { value = +value; // cast to number if(value) { return false; } let infValue = 1 / value; return infValue < 0; } isNegativeZero(0); // false isNegativeZero(-0); // true isNegativeZero('-0'); // true

#### 5. Applications

What is the use of knowing all this?

1. One example could be say for example if you are doing some machine learning and need to differentiate between positive and negative values for branching. If a -0 result gets coerced into a positive zero; then this could lead to a tricky branching bug.

2. Another usage scenario would be for people who write compilers and try to optimize code. Expressions that would result in zero e.g. x * 0 cannot be optimized as the result now depends on the value of x. Optimizing such expressions and replacing them with a 0 will lead to a bug.

3. And know too that there are lots of languages that support IEEE 754. Let’s take C# and Java for example:

// Java System.out.print(1.0 / 0.0); // Infinity System.out.print(1.0 / -0.0); // -Infinity

// C# Console.WriteLine(1.0 / 0.0); // Infinity Console.WriteLine(1.0 / -0.0); // -Infinity;

Try it in your language too!

### 6. IEEE specifications

The IEEE specifications lead to the following results

Math.round(-0.4); // -0 Math.round(0.4); // 0 Math.sqrt(-0); // -0 Math.sqrt(0); // 0 1 / -Infinity; // -0 1 / Infinity; // 0

Rounding -0.4 leads to -0 because it is viewed as the limit of a value as it approaches 0 from the negative direction.

The square root rule is one I find strange; the specification says: “Except that squareRoot(–0) shall be –0, every valid squareRoot shall have a positive sign.”. If you are wondering, IEEE 754 is the same reason why 0.1 + 0.2 != 0.3 in most languages; but that’s another story.

Thoughts? Do share them in the comments.

Great article! One other use-case I often talk about with -0 is in gaming or animations, where the magnitude of a number represents its speed and the sign represents its direction. -0 means “not moving, but pointing left” where 0 means “not moving, but pointing right”. I’ve seen this concept used in code that represents your vehicle on a GPS map, for example.

LikeLiked by 3 people

Wow, that’s amazing!

LikeLiked by 2 people

While the fact that there are indeed 2 zero values in javascript… I feel like this article is a little misleading. Specifically, the example usages you provide. For example, these 2:

“`

// example 1

posZero = +0;

negZero = -0;

“`

“`

// example 2

let a = ‘-0’;

a.toString(); // ‘0’

parseInt(‘-0’, 10); // -0

parseFloat(‘-0’, 10); // -0

“`

Here’s the problem:

– The literal usages of the plus (+) and minus (-) characters in the examples you provide are considered “Unary operators”.

– A unary operation is operation with only one operand.

– reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators#Unary_operators)

– Minus character (-) is an “unary negation” operator

– Plus character (+) is an “unary plus” operator

– Both of these operations (+0 and -0) do indeed return different values.

– 0

– -0

– However, you actually NEVER have direct access to the negative zero value (-0).

– You can only ever retrieve the negative zero value by:

– Running the unary negation operator on 0. (which is what you’ve shown already)

– storing a reference to the negative zero value in a pointer.

– When you write -0 in your javascript code it is NOT the same as a “negative zero value”.

I’m sure this seems like a rather trivial matter. However, as somebody who is self-taught – I can tell you that these details can really matter and ultimately can be slightly destructive for very new javascripters).

Tldr; When you write -0 in your javascript code it is NOT the same as a “negative zero value”. Instead, you are running a unary operation on the zero value, which outputs a negative zero value.

LikeLiked by 2 people

Thanks hulkish for the feedback. I’ll clarify some more.

1. First, parseInt and parseFloat take in string representations and give the number representation. The input value is the string ‘-0’ and not the numeric value. The output however is the negative zero value.

2. Yes, you can have a direct access to negative zero. In fact if you read the addition laws in the ES5 spec, there is an entry that specifies that exactly. Here is the link to the annotated spec: https://es5.github.io/#x11.6.3

3. Yes there are the unary negation and addition operators however they do not come into play when you are doing addition and subtraction. Regarding the operators, I wrote about them here:https://abdulapopoola.com/2015/09/30/why-javascript-seems-to-get-addition-wrong/.

LikeLiked by 2 people

Cool. Recently I am reading the book called “JavaScript, the definitive guide”. The author didn’t explain why there’re two zeros existed in JS, so I guess that doesn’t matter a lot. Now at least I know there’s actually a need to differentiate those two 0s. Also thanks for letting me know that they’re simply two opposite ways of approaching 0, although I’m not sure what is the correct binary code to represent -0.

LikeLiked by 2 people

[…] Why JavaScript has two zeros: -0 and +0 […]

LikeLike

[…] Why JavaScript has two zeros: -0 and +0 […]

LikeLike

[…] Why JavaScript has two zeros: -0 and +0 […]

LikeLike

[…] Why JavaScript has two zeros: -0 and +0 […]

LikeLike

[…] Why JavaScript has two zeros: -0 and +0 […]

LikeLike