DEV Community

Discussion on: Identifying Negative Zero

Collapse
 
rubberduck profile image
Christopher McClellan

Javascript uses IEEE 754 floating point numbers where the most significant (left most) bit represents the sign of the number. Therefore, +0 is encoded something like 0000 while -0 is something like 1000.

I don’t know JS very well, so I’m not sure you can access this raw, underlying representation, but in theory, you can bitwise and the input with an actual negative zero to efficiently check for equality.

Collapse
 
emnudge profile image
EmNudge • Edited

I did some testing and unfortunately could not get them to produce different results with bitwise operators.

I have been looking at the spec for how it decides what to do and it seems that unless explicitly stated otherwise, -0 and 0 are treated the same.

Collapse
 
rubberduck profile image
Christopher McClellan

That’s unfortunate.