+ 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

29th Apr 2024, 6:30 PM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
11 Respuestas
+ 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 :)
30th Apr 2024, 5:15 AM
Tibor Santa
Tibor Santa - avatar
+ 6
Is it a bug? Isn't the value of 0 the same as -0? If not, how are they different?
29th Apr 2024, 7:15 PM
Ausgrindtube
Ausgrindtube - avatar
+ 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
30th Apr 2024, 2:06 AM
ODLNT
ODLNT - avatar
+ 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()?
30th Apr 2024, 9:37 AM
Solo
Solo - avatar
+ 2
Tibor Santa I appreciate that SIR. Is there any way I can do this without using is method?
30th Apr 2024, 5:32 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 2
I found this. https://2ality.com/2012/03/signedzero.html function isNegativeZero(x) { return x === 0 && (1/x < 0); }
30th Apr 2024, 7:10 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Solo Thanks 😁
30th Apr 2024, 9:58 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 2
Yeah, nice tricks
30th Apr 2024, 12:09 PM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 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.
30th Apr 2024, 2:03 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 1
Tibor Santa Thanks sir
30th Apr 2024, 9:58 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
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!
30th Apr 2024, 2:11 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar