I think it's worth writing about motivation and possible use cases.
I think this sentence is very suitable for the case when you need to compare the signs of two numbers in proper way:
const sameSigns = Math.signbit(x) === Math.signbit(y);
const diffSigns = Math.signbit(x) !== Math.signbit(y);
Why Math.sign is not works in this case?
- if
x == +0 and y == -0 Math.sign(x) === Math.sign(y) will return true. See why this important.
- if one of variables
x or y is NaN Math.sign(x) === Math.sign(y) will always return false.
When we need compare signs? It's pretty often required in computational geometry:
https://github.com/search?q=SAME_SIGNS&type=Code
I think it's worth writing about motivation and possible use cases.
I think this sentence is very suitable for the case when you need to compare the signs of two numbers in proper way:
Why
Math.signis not works in this case?x==+0andy==-0Math.sign(x) === Math.sign(y)will returntrue. See why this important.xoryisNaNMath.sign(x) === Math.sign(y)will always returnfalse.When we need compare signs? It's pretty often required in computational geometry:
https://github.com/search?q=SAME_SIGNS&type=Code