Raynos
Raynos

Reputation: 169383

Does javascript have a concept of negative zero

Consider the following

var l = console.log.bind(console);
l(-0); // 0
l(0); // 0
l(0 === -0); // true
l(0 == -0); // true
l(1 / 0); // Infinity
l(1 / -0); // -Infinity

Bonus question:

I know NaN/NaN is a combination where non-equal objects behave the same.

Upvotes: 6

Views: 619

Answers (1)

kennytm
kennytm

Reputation: 523224

Why is negative zero equal to zero ?

Because IEEE 754 demands it.

Is the 0/-0 combination the only combination where equal objects behave differently?

I believe so. In Javascript, only Numbers have a special === algorithm, and 0, -0, NaN are the only special cases there (ECMA-262 §11.9.6).

Upvotes: 8

Related Questions