PHP 减法返回无限数
我有一个存储数据的数组。如果我减去两个数组,我会得到一个无限大的数字。这是一个示例
$i[1] = 2.14;
$i[2] = 2.15;
$diff = $i[1] - $i[2];
echo $diff;
此代码的输出应该是 -1
但我得到的是 -0.0099999999999998
?对于我正在编写的代码,我需要准确的数字。有谁知道为什么会发生这种情况以及我该如何解决它?
谢谢
I have an array that store data. If I subtract two arrays I get an infinately big number. Here is an example
$i[1] = 2.14;
$i[2] = 2.15;
$diff = $i[1] - $i[2];
echo $diff;
The output of this code should be -1
but instead I am getting -0.0099999999999998
? With the code I am making I need the numbers to be exact. Does anyone know why this is happening and how I can fix it?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为浮点运算中引入了不准确性。
对于任意精度运算,请参阅手册中的BCMath。
This is because of inaccuracies introduced in floating point operations.
For arbitrary precision operations see BCMath in the manual.