floating-point - PHP 8 浮点小数点不同于 PHP 7

标签 floating-point precision php-7.4 php-8 php-8.1

我将一些会计脚本升级到 PHP 8.1,但是我得到的 float 不正确。

我循环了一些交易借方/贷方并且余额为 0 但是最后当我尝试比较零时它不适用于 == 0 和 === 0

经过几次调试我发现:

<?php
var_dump(1097.5 - 835.7);

返回 float (261.79999999999995)

PHP 7 中的同一行返回 float (261.8)

这可以很容易地使用这个 sandbox 进行测试

所以我想知道是否可以在我的 PHP 配置中更改任何设置,以确保我获得与之前相同的 float 结果,而不必在数百个脚本的每个阶段都进行四舍五入?

最佳答案

PHP 8.0 UPGRADE NOTES :

var_dump() and debug_zval_dump() will now print floating-point numbers using serialize_precision rather than precision. In a default configuration, this means that floating-point numbers are now printed with full accuracy by these debugging functions.

所以你可以改变这个

ini_set('serialize_precision', 16);

https://3v4l.org/uOAPD#v8.1rc3

但是,我怀疑这是您的真正问题!因为此更改仅影响“这些调试功能”以及 serializejson_encode

等序列化功能

关于floating-point - PHP 8 浮点小数点不同于 PHP 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69858549/

相关文章:

c - C中分数简化程序的浮点异常

c++ - 'const' 双重复制+比较安全吗?

c++ - 将 float 分配给 long double 时, float 的值会发生什么变化?

math - 是否可以预测数学运算是否会导致溢出?

r - 使用 Rmpfr 在 R 中进行高精度计算

c# - 无关代码更改计算结果

php - AMPHP - 排队的任务多于池中可用的工作人员

apache2.4 - 无法加载模块 'ext\\php_curl.dll',因为它与 9.0 链接

math - float 学坏了吗?