2.9999999999999999 >> .5?

后端 未结 10 1841
逝去的感伤
逝去的感伤 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:27

    It should be noted that the number ".0000000000000007779553950749686919152736663818359374" is quite possibly the Epsilon, defined as "the smallest number E such that (1+E) > 1."

    0 讨论(0)
  • 2021-02-07 16:29

    The shift right operator only operates on integers (both sides). So, shifting right by .5 bits should be exactly equivalent to shifting right by 0 bits. And, the left hand side is converted to an integer before the shift operation, which does the same thing as Math.floor().

    0 讨论(0)
  • 2021-02-07 16:33

    I suspect that converting 2.9999999999999997779553950749686919152736663818359374999999 to its binary representation would be enlightening. It's probably only 1 bit different from true 3.

    0 讨论(0)
  • 2021-02-07 16:34

    Try this javascript out: alert(parseFloat("2.9999999999999997779553950749686919152736663818359374999999"));

    Then try this: alert(parseFloat("2.9999999999999997779553950749686919152736663818359375"));

    What you are seeing is simple floating point inaccuracy. For more information about that, see this for example: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems.

    The basic issue is that the closest that a floating point value can get to representing the second number is greater than or equal to 3, whereas the closes that the a float can get to the first number is strictly less than three.

    As for why right shifting by 0.5 does anything sane at all, it seems that 0.5 is just itself getting converted to an int (0) beforehand. Then the original float (2.999...) is getting converted to an int by truncation, as usual.

    0 讨论(0)
提交回复
热议问题