As usual, I like to delve into some of the weird or complicated bits of JS. The other week I was thinking about a way to possibly identify a negat...
For further actions, you may consider blocking this person and/or reporting abuse
You could also use
Object.is()
, which does make a difference between-0
and0
:Yes! I completely forgot to include that one!
Object.is() seems to be a "fixed" === since it also works when comparing NaN, unlike ===.
MDN gives a polyfill code:
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 like1000
.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.
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
and0
are treated the same.That’s unfortunate.
A while back I had an interesting use case for
-0
. I was looking for a way to map x,y coordinates into absolute positioning of an element in CSS. And I wanted that behavior to be different for positive and negative numbers, to create an easy interface for the consumer of that component. So negative x would position the element from the right edge, positive x would position from the left edge. Vice versa for the y axis.I ended up writing a small function that would determine if the number provided is negative, including
-0
:And this function was used to position the element with some simple logic:
I maybe actually found a usecase for
-0
: Minecraft world coords. The coordinates0 0
are on the corner between four blocks, meaning there are four columns of blocks at0 0
. So if you have a tool written in JS that works based on coords in Minecraft or a similar game, having a distinction between-0
and0
can save you from some errors adjacent to the coordinate axes.Although if you use floating point instead of integer coords,
-0
and0
sit in the exact same spot in the world.Be carefull with this one -
return zero.toLocaleString()[0] === "-"
.For some locales on certain OS minus character is going to be different (e.g. char code 8722 instead of 45), so this comparison will be false for -0. For example that's how it works for Swedish on Windows.
You can create a very small sign function which works with
-0
and0
:For everything except Infinity
Math.sign(1 / x)
is sufficient. To support infinite values the|| x
is needed because1 / Infinity
is0
.Thanks for sharing! Found this article because I was converting decimal coordinates to degrees/minutes/seconds coordinates. Pulling the degree out as the integer number before the decimal was causing the negative sign to be lost when it was converted back into a string.
In unusual and rare circumstances, a couple of Math methods (asinh) will output negative zero when given zero as an input. For bugfixes, the only way to check for it is by this method.