Negative zero check

    Today I found that the Math.atan2 function returns a different result depending on which character to pass to 0. It works because JavaScript stores numbers according to the IEEE 754 standard , in which all numbers, including zero, have a sign. Just yesterday, a certain Allen Wirfs-Brock drew attention to the same feature . His method of verification is even simpler: There is, of course, no practical benefit from this, except to demonstrate his deep knowledge of the language.

    > 0 === -0           // => true
    > Math.atan2(0, -0)  // => 3.141592653589793
    > Math.atan2(-0, -0) // => -3.141592653589793




    function isNegative0(n) {
      return n === 0 && (1 / n) === -Infinity
    }



    Also popular now: