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

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

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:

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?

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:

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?

Share Improve this question asked Oct 19, 2015 at 0:06 Tomáš ZatoTomáš Zato 53.5k63 gold badges310 silver badges828 bronze badges 3
  • 2 en.wikipedia/wiki/Signed_zero – zerkms Commented Oct 19, 2015 at 0:11
  • 2 Related Are +0 and -0 the same? – Felix Kling Commented Oct 19, 2015 at 0:11
  • An important thing to note is that -0 === 0 returns true, so it's not something you have to worry about. If you want to transform -0 into 0, just write myvar = myvar || 0; or do any operation on it. (-0).toString() returns "0" too – Domino Commented Oct 19, 2015 at 0:25
Add a ment  | 

3 Answers 3

Reset to default 5

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 bee 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 pact 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.)

ECMAScript has two zero numbers: +0 and -0.

They are considered equal by most parison algorithms:

+0 == -0          // true (Equality parison)
+0 === -0         // true (StrictEquality parison)
[+0].includes(-0) // true (SameValueZero parison)

However, they are different number values:

Object.is(+0, -0) // false (SameValue parison)

Usually, both zeros behave identically in mathematical calculations. However,

1 / +0 === +Infinity
1 / -0 === -Infinity

In this case, you get -0 because Math.round is defined as follows:

If x is less than 0 but greater than or equal to -0.5, the result is −0.

In general, if you have an integer between −231 and 231−1, and you want to convert it to +0 if it's -0, and don't alter it otherwise, you can use bitwise OR:

-1 | 0 // -1
-0 | 0 // +0
+0 | 0 // +0
+1 | 0 // +1

However, in your case you could just take the -1 outside of Math.round:

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

But note using Math.round will produce a non-uniform probability distribution. Consider using

Math.floor(Math.random()*3) - 1

The function Math.round works as specified in the standard:

If x is less than 0 but greater than or equal to -0.5, the result is −0.

Math.random returns number between 0 (zero) and 1 (one) exclusively, so you will have several occurances of that rule.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744946510a4602653.html

相关推荐

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

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

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信