开发者

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
  • Why is negative zero equal to zero ?
  • Given it's equal开发者_JAVA百科 why does it behave differently ?

Bonus question:

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

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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜