+ 6
[CLEARIFIED] 0 === -0 and 0 == -0
I write a code on JS to check those conditions but JavaScript show TRUE for both of this. I think this is a kind of BUG in jS, right? To solve this BUG js created a is method, that I include in the code, check please. This method is worked as like '===' work, in same way, just we can compare 0 and -0 preciously. Is there any way to solve this in javascript without using is method? https://sololearn.com/compiler-playground/c65zPEjWTb62/?ref=app
11 Answers
+ 6
You cannot really call something a bug, which works according to the language specification.
Maybe the design can be questionable (in case of Javascript there are even more dubious issues), but as consultants say, it a feature not a bug :)
+ 6
Is it a bug? Isn't the value of 0 the same as -0? If not, how are they different?
+ 5
In JavaScript, positive and negative zero are considered equal when using the strict equality operator (===), as per the IEEE 754 standard for floating-point arithmetic.
https://phind.com/search?cache=shi7w3od6snwjhn4enrrnifp
+ 3
`нттp⁴⁰⁶ "if you really want to, you can fly into space"...😎:
console.log(1/0 == 1/-0); // false
I hope that's what you meant when you asked about a different replacement method Object.is()?
+ 2
Tibor Santa I appreciate that SIR. Is there any way I can do this without using is method?
+ 2
I found this.
https://2ality.com/2012/03/signedzero.html
function isNegativeZero(x) {
return x === 0 && (1/x < 0);
}
+ 2
Solo Thanks 😁
+ 2
Yeah, nice tricks
+ 1
Sir Ausgrindtube, in many programming languages, -0 is represented as negative zero, which is equivalent to zero in terms of mathematical operations but may have different behavior in certain operations involving floating-point numbers, such as division by zero or comparison. Here the anomaly related to comparison. They have different representations in memory or when printed/displayed due to internal representations of floating-point numbers or signed zeros and 0 is counted as unsigned zeros. Specially unsigned numbers don't have any sign, these can contain only magnitude of the number whereas signed numbers do have. So, they are different. One is unsigned number and another is signed number to computer.
+ 1
Tibor Santa Thanks sir
0
ODLNT I know but to fixed this bug they provide is method. I do use in my code that I attached. But my question is, are there any methods available rather than is I can do that. Coz a signed number can't be equal to an unsigned number!