Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-0 in JavaScript after using Math.round? What is -0?

Tags:

javascript

I just wanted to generate random integer number in range <-1,1>, that is -1, 0 or 1. I think this code nails it:

Math.round(Math.random()*2-1)

But I'm getting one more value apart from -1, 0 and 1. I'm not sure what is that supposed to be:

image description

Now I don't think this is implementation error, but if it is, I'm using Firefox 41 and same thing happened in Google Chrome 39. When I tried to log -0 in console it appeared as -0 but -0 converted to String appears as "0". What is it? Can I use it for something, like some cool programming hacks and workarounds? Can it cause some errors if I don't just ignore it?

like image 539
Tomáš Zato - Reinstate Monica Avatar asked Dec 25 '22 13:12

Tomáš Zato - Reinstate Monica


1 Answers

Two Zeros

Because JavaScript’s numbers keep magnitude and sign separate, each nonnegative number has a negative, including 0. The rationale for this is that whenever you represent a number digitally, it can become so small that it is indistinguishable from 0, because the encoding is not precise enough to represent the difference. Then a signed zero allows you to record “from which direction” you approached zero; that is, what sign the number had before it was considered zero.

from here


More technical stuff if you are interested.

The implementation of -0 and +0 is introduced in 1985 by IEEE as part of the IEEE 754 standard. The standard addressed many problems found in the diverse floating point implementations that made them difficult to use reliably and portably.

The standard defines

  • arithmetic formats: sets of binary and decimal floating-point data, which consist of finite numbers (including signed zeros and subnormal numbers), infinities, and special "not a number" values (NaNs)
  • interchange formats: encodings (bit strings) that may be used to exchange floating-point data in an efficient and compact form
  • rounding rules: properties to be satisfied when rounding numbers during arithmetic and conversions
  • operations: arithmetic and other operations on arithmetic formats exception handling: indications of exceptional conditions (such as division by zero, overflow, etc.)
like image 116
kiranvj Avatar answered Dec 27 '22 02:12

kiranvj