PHP 用负数计算给出了错误的值 [英] PHP calculating with negative numbers gives wrong values

查看:64
本文介绍了PHP 用负数计算给出了错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 PHP 代码:

echo -100.35+100.00;
echo '<br/>';
echo -478.35+478.32+0.03;

给出输出:

-0.34999999999999
-2.9559688030645E-14

我不明白为什么,我尝试在值前面(浮动)但没有区别.

I don't understand why, i tried (float) in front of the values but no difference.

推荐答案

看看 本手册,它解释了浮点数的内部表示并非 100% 准确……这与我怀疑的浮点数的内部表示有关.

Take a look at this manual, it explains that the internal representation is not 100% exact for floats... it has to do with the internal representation of the floats I suspect.

手册中也提到了这些方法这些用于需要精度的计算.

The manual does also refer to these methods and also these for calculations with need of precision.

这篇关于PHP 用负数计算给出了错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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