negativeZero.js - tiny JavaScript function to differentiate -0 from +0 (can process any type other than Symbol or BigInt)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// negativeZero.js - tiny JavaScript function to differentiate -0 from +0 (can process any type other than Symbol or BigInt) | |
// 15 chars: N=a=>1/0==1/-a; | |
var negativeZero = v => Infinity === 1 / -v; // => v === 0 && Number.NEGATIVE_INFINITY === 1 / v; | |
var isWeb = Date.now; // JScript is missing this method | |
var values = [ | |
-0, | |
"-0", | |
-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, | |
"-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", | |
-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001, | |
"-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", | |
0, | |
"0", | |
-Infinity, | |
"-Infinity", | |
Infinity, | |
"Infinity", | |
-1, | |
"-1", | |
1, | |
"1", | |
-7, | |
"-7", | |
7, | |
"7", | |
NaN, | |
"Z", | |
"", | |
[-0], | |
{}, | |
new Date(), | |
Date, | |
/foo/img, | |
false, | |
true, | |
null, | |
void 0 | |
].concat( | |
isWeb ? [ | |
new BigUint64Array(), | |
new Int8Array(), | |
new Map(), | |
new Set(), | |
eval("()=>{}"), | |
eval("( () => function*() {} )()"), | |
eval("new Promise(()=>{})"), | |
// Symbol(), // TypeError: Cannot convert a Symbol value to a number | |
// BigInt(-0), // TypeError: Cannot convert a BigInt value to a number | |
// BigInt(0), | |
// BigInt(-Infinity), | |
// BigInt(Infinity), | |
// BigInt(-1), | |
// BigInt(1), | |
// BigInt(-7), | |
// BigInt(7), | |
] | |
: [] | |
); | |
if(isWeb) { | |
eval("values.forEach( v => console.log( negativeZero(v) ) );"); | |
} else { // JScript-compatible... | |
// 32 chars: N=function(a){return 1/0==1/-a}; | |
var negativeZero_JScript = function(v) { | |
return Infinity === 1 / -v; // return v === 0 && Number.NEGATIVE_INFINITY === 1 / v; | |
}; | |
console = { log: function() { var a = arguments; if(a.length) WScript.Echo( [].join.call(a, " ") ); } }; | |
for(var i in values) console.log( negativeZero_JScript(values[i]) ); | |
}; | |
// (4) true | |
// (35) false (28 in JScript) | |
// NOTE: "If all else fails, add zero." | |
// https://stackoverflow.com/questions/7223359/are-0-and-0-the-same/53135516#53135516 | |
console.log([ | |
negativeZero( Math.round(-0.0001) ), // true | |
negativeZero( Math.trunc(-0.0001) ), // true | |
negativeZero( Math.ceil(-0.0001) ), // true | |
negativeZero( Math.ceil(-0.0001) +0 ), // false | |
negativeZero( Math.round(-0.0001) +0 ), // false | |
negativeZero( Math.ceil(-0.0001) +0 ), // false | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment