2.9999999999999999 >> .5?

后端 未结 10 2200
悲&欢浪女
悲&欢浪女 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: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

提交回复
热议问题