2.9999999999999999 >> .5?

后端 未结 10 1856
逝去的感伤
逝去的感伤 2021-02-07 15:32

I heard that you could right-shift a number by .5 instead of using Math.floor(). I decided to check its limits to make sure that it was a suitable replacement, so I checked the

10条回答
  •  花落未央
    2021-02-07 16:13

    Actually, you're simply ending up doing a floor() on the first operand, without any floating point operations going on. Since the left shift and right shift bitwise operations only make sense with integer operands, the JavaScript engine is converting the two operands to integers first:

    2.999999 >> 0.5
    

    Becomes:

    Math.floor(2.999999) >> Math.floor(0.5)
    

    Which in turn is:

    2 >> 0
    

    Shifting by 0 bits means "don't do a shift" and therefore you end up with the first operand, simply truncated to an integer.

    The SpiderMonkey source code has:

    switch (op) {
      case JSOP_LSH:
      case JSOP_RSH:
        if (!js_DoubleToECMAInt32(cx, d, &i)) // Same as Math.floor()
            return JS_FALSE;
        if (!js_DoubleToECMAInt32(cx, d2, &j)) // Same as Math.floor()
            return JS_FALSE;
        j &= 31;
        d = (op == JSOP_LSH) ? i << j : i >> j;
        break;
    

    Your seeing a "rounding up" with certain numbers is due to the fact the JavaScript engine can't handle decimal digits beyond a certain precision and therefore your number ends up getting rounded up to the next integer. Try this in your browser:

    alert(2.999999999999999);
    

    You'll get 2.999999999999999. Now try adding one more 9:

    alert(2.9999999999999999);
    

    You'll get a 3.

提交回复
热议问题