感谢堆栈,我了解了浮点不精度,所以我转到了 bc 函数。
这在“普通”浮点数上效果很好,但是对于非常小的浮点数,比如10^-10
类型,bcadd
总是给出0
.
有人可以告诉我我做错了什么来使这些小花车精确添加吗?
提前谢谢了!
PHP
$numerator = 1;
$denominator = 1000000000;
$quotientOne = $numerator / $denominator;
$numerator = 1;
$denominator = 1000000000000000;
$quotientTwo = $numerator / $denominator;
$smallSum = bcadd($quotientOne, $quotientTwo, 100);
echo $quotientOne . "<br>";
echo $quotientTwo . "<br>";
echo $smallSum . "<br>";
给
1.0E-9
1.0E-15
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000