2.9999999999999999 >> .5?

后端 未结 10 2189
悲&欢浪女
悲&欢浪女 2021-02-07 16:04

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:26

    And to add to John's answer, the odds of this being more performant than Math.floor are vanishingly small.

    I don't know if JavaScript uses floating-point numbers or some kind of infinite-precision library, but either way, you're going to get rounding errors on an operation like this -- even if it's pretty well defined.

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

    This is possibly the single worst idea I have ever seen. Its only possible purpose for existing is for winning an obfusticated code contest. There's no significance to the long numbers you posted -- they're an artifact of the underlying floating-point implementation, filtered through god-knows how many intermediate layers. Bit-shifting by a fractional number of bytes is insane and I'm surprised it doesn't raise an exception -- but that's Javascript, always willing to redefine "insane".

    If I were you, I'd avoid ever using this "feature". Its only value is as a possible root cause for an unusual error condition. Use Math.floor() and take pity on the next programmer who will maintain the code.


    Confirming a couple suspicions I had when reading the question:

    • Right-shifting any fractional number x by any fractional number y will simply truncate x, giving the same result as Math.floor() while thoroughly confusing the reader.
    • 2.999999999999999777955395074968691915... is simply the largest number that can be differentiated from "3". Try evaluating it by itself -- if you add anything to it, it will evaluate to 3. This is an artifact of the browser and local system's floating-point implementation.
    0 讨论(0)
  • 2021-02-07 16:31

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

    Good guess, but no cigar. As the double precision FP number has 53 bits, the last FP number before 3 is actually (exact): 2.999999999999999555910790149937383830547332763671875

    But why it is 2.9999999999999997779553950749686919152736663818359375

    (and this is exact, not 49999... !)

    which is higher than the last displayable unit ? Rounding. The conversion routine (String to number) simply is correctly programmed to round the input the the next floating point number.

    2.999999999999999555910790149937383830547332763671875

    .......(values between, increasing) -> round down

    2.9999999999999997779553950749686919152736663818359375

    ....... (values between, increasing) -> round up to 3

    3

    The conversion input must use full precision. If the number is exactly the half between those two fp numbers (which is 2.9999999999999997779553950749686919152736663818359375) the rounding depends on the setted flags. The default rounding is round to even, meaning that the number will be rounded to the next even number.

    Now

    3 = 11. (binary)

    2.999... = 10.11111111111...... (binary)

    All bits are set, the number is always odd. That means that the exact half number will be rounded up, so you are getting the strange .....49999 period because it must be smaller than the exact half to be distinguishable from 3.

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

    I don't think your right shift is relevant. You are simply beyond the resolution of a double precision floating point constant.

    In Chrome:

    var x = 2.999999999999999777955395074968691915273666381835937499999;
    var y = 2.9999999999999997779553950749686919152736663818359375;
    
    document.write("x=" + x);
    document.write(" y=" + y);
    

    Prints out: x = 2.9999999999999996 y=3

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