I ❤ javascript: What’s the sense of having +0/-0 in JS?

Hi! Do you know what’s the result of the following
equations? I’ll tell you, the answer is -0. Yes, But why? Let’s clear it up. If this is your first time here and you want
to learn Frontend and JavaScript, start right now by subscribing, and don’t forget to turn
on the subtitles. [Music playing] According to the ECMAScript Standard,
in JavaScript, all numbers are 64-bit floating-point numbers, encoded in double precision following
the IEEE 754 Standard. That standard handles the sign like sign-and-magnitude
encoding for integers, therefore it has a signed zero. To represent numbers in a binary format one
bit is used for a sign. Therefore, these two signed zeros have a sense
from the engineering perspective. It’s easier to implement and faster to compute. According to the following paper
The division by zero is not allowed. However, it can be represented as the following
limit, where X approaches 0 from the positive or negative side. By introducing a signed zero, this Standard
uses the sign of a “zero” to indicate the direction the limit is being approached from. The following equation is legal in JavaScript But those symbols have special meanings, not
“zero” or “Infinity.” The ” Positive Infinity” means a real number
that is larger than we can otherwise represent in our floating-point symbol set. And a “positive zero” is an actual number
that is larger than true 0 but smaller than any positive number we can represent. According to the JavaScript standard, it hides a signed zero. But for mathematical operations doesn’t. Moreover, if you need to distinguish between
positive and negative zeros you cannot use the “equality” operation. You need to use “Object.is” method, presented
in ES6. One more point to remember. The result of the following is “true.” That’s correct, “Number.MIN_VALUE” yields
a tiny number, but it’s greater than zero. It represents the smallest positive number not the smallest negative number. The same “two zeros” you can see in C, C#,
Swift, and other programming languages. That’s it. So easy. [Music playing] If you like this video give it “thumbs up”,
share it with your friends, subscribe to the channel and watch other episodes. Thanks for watching and dive deeper.

Leave a Reply

Your email address will not be published. Required fields are marked *