ennvlxkm
2 months ago  Karma: 713
Negative zero in JavaScript

Does anyone know why does JavaScript support both normal zero and negative zero?

en
#javascript
ennvlxkm
2 months ago  Karma: 713
Negative zero in JavaScript

Does anyone know why does JavaScript support both normal zero and negative zero?

en
#javascript

2 ANSWERS
clam0
2 months ago Karma: 3307

int x = -0
int y = +0

x === y evaluates to true , so javascript treats them as the same number.

" In the IEEE 754 standard used to represent the Number type in JavaScript, the sign is represented by a bit (a 1 indicates a negative number).

As a result, there exists both a negative and a positive value for each representable number, including 0.

This is why both -0 and +0 exist."

int x = -0
int y = +0

x === y evaluates to true , so javascript treats them as the same number.

" In the IEEE 754 standard used to represent the Number type in JavaScript, the sign is represented by a bit (a 1 indicates a negative number).

As a result, there exists both a negative and a positive value for each representable number, including 0.

This is why both -0 and +0 exist."

kayden.hughes
2 months ago Karma: 1470

It can be used in several scenarios to prevent unwanted information loss.
Like the speed of movement per animation frame where the sign could represent the direction of the movement. If the sign is lost, you will have no way of knowing what direction it was moving in before getting at zero.

It can be used in several scenarios to prevent unwanted information loss.
Like the speed of movement per animation frame where the sign could represent the direction of the movement. If the sign is lost, you will have no way of knowing what direction it was moving in before getting at zero.

Earn tokens by posting and answering questions about blockchain!
Karma to eth
YOUR ANSWER