Are +0 and -0 the same?

后端 未结 9 2050
花落未央
花落未央 2020-11-22 07:45

Reading through the ECMAScript 5.1 specification, +0 and -0 are distinguished.

Why then does +0 === -0 evaluate to true<

9条回答
  •  时光取名叫无心
    2020-11-22 08:05

    JavaScript uses IEEE 754 standard to represent numbers. From Wikipedia:

    Signed zero is zero with an associated sign. In ordinary arithmetic, −0 = +0 = 0. However, in computing, some number representations allow for the existence of two zeros, often denoted by −0 (negative zero) and +0 (positive zero). This occurs in some signed number representations for integers, and in most floating point number representations. The number 0 is usually encoded as +0, but can be represented by either +0 or −0.

    The IEEE 754 standard for floating point arithmetic (presently used by most computers and programming languages that support floating point numbers) requires both +0 and −0. The zeroes can be considered as a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞, division by zero is only undefined for ±0/±0 and ±∞/±∞.

    The article contains further information about the different representations.

    So this is the reason why, technically, both zeros have to be distinguished.

    However, +0 === -0 evaluates to true. Why is that (...) ?

    This behaviour is explicitly defined in section 11.9.6, the Strict Equality Comparison Algorithm (emphasis partly mine):

    The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

    (...)

    • If Type(x) is Number, then

      1. If x is NaN, return false.
      2. If y is NaN, return false.
      3. If x is the same Number value as y, return true.
      4. If x is +0 and y is −0, return true.
      5. If x is −0 and y is +0, return true.
      6. Return false.

    (...)

    (The same holds for +0 == -0 btw.)

    It seems logically to treat +0 and -0 as equal. Otherwise we would have to take this into account in our code and I, personally, don't want to do that ;)


    Note:

    ES2015 introduces a new comparison method, Object.is. Object.is explicitly distinguishes between -0 and +0:

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

提交回复
热议问题