Abs()-PHP中的绝对值函数问题 [英] Abs() - issue with absolute value function in PHP

查看:96
本文介绍了Abs()-PHP中的绝对值函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么此代码针对abs()(绝对值)执行此操作- 在我的代码中它将显示'GREATER'-尽管0.50永远不会大于0.5,但是我在这里是否使用abs函数错过了某些东西?

Can anyone explain why this code does this in regards to abs() (absolute value) - In my code it will display 'GREATER' - although 0.50 is never GREATER than 0.5, am I missing out something here with the abs function?

$logic = abs(1.83333333333 - 2.33333333333);  // 0.50
$limit = 0.50;

if ($logic > $limit) {
    echo 'IS GREATER';
} else {
    echo 'IS NOT GREATER';
}

推荐答案

将浮点数传递给ab,您将得到一个浮点数.在这种情况下,您可能会遇到浮点表示形式的问题:浮点永远不会绝对精确,因此,您很可能会得到一个不完全为0.50的数字,而是类似0.500000 ... 01的数字.您可以尝试使用php round函数将结果四舍五入到所需的精度(在您的情况下,我想是两个).

Passing floating point numbers to abs you will get a floating point number as result. In that case you can experience problems with the floating point representation: a floating point is never absolutely precise, thus you are most likely getting a number that is not exactly 0.50 but something like 0.500000...01. You could try to round the result to the desired precision (in your case I guess it is two) with the php round function.

这篇关于Abs()-PHP中的绝对值函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆