Why is there -0?

During experiments with JavaScript. I tested a few odd code snippets, here are some of my conclusions (to understand how I came across -0 ),

When doing +[] in the console, this returns 0 . I'm not sure why, but it is. So this means that the positive array has a value of 0 ..

After that, I became curious and tried the following:

 console.log(-[]); 

And that returns -0 ...

What is the meaning of -0? 0 and -0 are both irrelevant, so negation is really not necessary ... Or is it like that? Maybe JavaScript has a target for -0?

Adding to this. I can not find another way to play -0 , except using -0 itself, or using -[] ...


Some other strange finds, my question is about -0

 (-0) + (-0) = 0 (-0) - (-0) = -0 (-0) * (-0) = 0 (-0) / (-0) = NaN // of course 
+5
source share
2 answers

JavaScript uses the IEEE 754 standard to represent numbers. From Wikipedia :

The signed zero is zero with a sign. In ordinary arithmetic, -0 = +0 = 0. However, when calculating some representations of numbers, there are two zeros, often denoted by -0 (negative zero) and +0 (positive zero) . This happens in some numbers with numeric representations for integers and in most representations of floating point numbers. The number 0 is usually encoded as +0, but can be represented either +0 or -0.

The IEEE 754 standard for floating point arithmetic (currently used by most computers and programming languages ​​that support floating point numbers) requires both +0 and -0. Zeros can be considered as a variant of the extended line of real numbers, so 1 / -0 = -∞ and 1 / + 0 = + ∞, division by zero is only undefined for Β± 0 / Β± 0 and Β± ∞ / Β± ∞.

From This Post

+3
source

This will help determine what happens when you split it.

If you divide by +0 , you get +Infinity . If you divide by -0 , you get -Infinity . (For nonzero numerators, of course).

+2
source

Source: https://habr.com/ru/post/1207464/


All Articles