2.9999999999999999 >> .5?

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

    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.

提交回复
热议问题