dongtiao2105 2014-05-21 22:39
浏览 352
已采纳

整数除以100是否会导致PHP中出现浮点错误?

I'm always wary of adding and subtracting floating point numbers, as they can cause some errors when truncating instead of round.

My question though: will an integer divided by 100 ever cause floating point errors in PHP?

I.e. is it "safe", should I always rely on round($x / 100, 2)?

  • 写回答

1条回答 默认 最新

  • dpquu9206 2014-05-21 22:41
    关注

    Yes. Actually, since 100 isn't a power of two, the result of $x / 100 will always be inexact unless $x is a multiple of 25. (You get 4 for free since 4 is a power of 2.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?