加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – floor(24.24 * 1e4)返回错误的值

发布时间:2020-12-13 17:16:20 所属栏目:PHP教程 来源:网络整理
导读:执行一些坐标舍入,我来到一些bug或其他东西,不同的数字它可以根据我的需要工作 $res = floor(24.24*1e4)/1e4;echo $res; 返回24.2399 你知道这个242400有什么特别之处吗,它会返回242399吗? 解决方法 问题与浮点精度有关 尽管PHP手册中说“如果需要,通过舍入
执行一些坐标舍入,我来到一些bug或其他东西,不同的数字它可以根据我的需要工作

$res = floor(24.24*1e4)/1e4;
echo $res;

返回24.2399

你知道这个242400有什么特别之处吗,它会返回242399吗?

解决方法

问题与浮点精度有关

尽管PHP手册中说“如果需要,通过舍入值返回下一个最小的整数值”.如果你在floor()的PHP手册中进一步阅读页面中的“返回值”,你会看到:

value rounded to the next lowest integer. The return value of floor()
is still of type 07001 because the value range of float is usually
bigger than that of integer.

当我们检查float类型时,我们看到一个警告:

Floating point numbers have limited precision. Although it depends on the system,PHP typically uses the IEEE 754 double precision format,which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors,and,of course,error propagation must be considered when several operations are compounded.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读