I want to test if the result of an operation (or a number) is Negative Zero (-0).
Doing a comparison using ==
or ===
does not work because:
0 == -0 // true
0 === -0 // true
I have considered that a number divided by -0 will give -Infinity; so if say 1 is divided by the number to be tested results in -Infinity
, then the input number was -0
.
1/number == -Infinity; // true if number is -0
Is this a valid and sound/correct method to do the test; or is there an alternative better way for testing?
Also, is there any other number (other than -0) if 1 / number
will also give -Infinity? Therefore the necessity for testing first that the number is zero before the check is made that it is -0. i.e. as follows:
number ==0 && 1/number == -Infinity;
Thanks
function isMinusZero(number) {return 1/number == -Infinity;}
// ========= Test Cases ===================
console.log(isMinusZero(-0)) // TRUE
console.log(isMinusZero(-0-0)); // TRUE
console.log(isMinusZero("-0")); // TRUE
console.log(isMinusZero(55/-Infinity)); // TRUE (Number divided by -Infinity is -0)
console.log("--------------");
console.log(isMinusZero(0)); // FALSE
console.log(isMinusZero("0")); // FALSE
console.log(isMinusZero(4)); // FALSE
console.log(isMinusZero(-Infinity)); // FALSE