📜 ⬆️ ⬇️

Check for negative zero

Today I discovered that the Math.atan2 function returns a different result depending on which sign to pass 0 to.

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


This 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 Virfs-Brock noticed the same feature. His way of checking is even simpler:

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


Of course, there is no practical benefit from this, except to demonstrate your deep knowledge of the language.

')

Source: https://habr.com/ru/post/113971/


All Articles