douxunwei8259 2018-04-23 12:55
浏览 113
已采纳

PHP浮点精度:var_dump是否秘密舍入,我该如何调试precisley呢?

That floating point numbers in PHP are inaccurate is well known (http://php.net/manual/de/language.types.float.php), however I am a bit unsatisfied after the following experiment:

var_dump((2.30 * 100)); // float(230)
var_dump(round(2.30 * 100)); // float(230)
var_dump(ceil(2.30 * 100)); // float(230)
var_dump(intval(2.30 * 100)); // int(229)
var_dump((int)(2.30 * 100)); // int(229)
var_dump(floor(2.30 * 100)); // float(229)

The internal representation must be something like 229.999998.

var_dump((2.30 * -100)); // float(-230)
var_dump(round(2.30 * -100)); // float(-230)
var_dump(ceil(2.30 * -100)); // float(-229)
var_dump(intval(2.30 * -100)); // int(-229)
var_dump((int)(2.30 * -100)); // int(-229)
var_dump(floor(2.30 * -100)); // float(-230)

The internal representation must be something like -229.999998.

Ok as far as I understand - integer casting as well as the intval function simply cut of everything behind the point. Good to know.

However var_dump() gives me a value of 230 though the real value must be different according to those results.

Now have a look at this:

$a = 230.0;

var_dump($a); // float(230)
var_dump((int) $a); // int(230)

That means the internal representation of the floating point number must be different here. If I want to know the exact value of a float therefore I can not debug using var_dump as I am used to right? How can I debug an exact float value?

  • 写回答

1条回答 默认 最新

  • dongzhuxun5136 2018-04-23 13:51
    关注

    You can try to use number_format it won't be perfect since you have to provide number of decimals, but should help.

    echo number_format(8-6.4, 50);
    

    1.59999999999999964472863211994990706443786621093750

    echo number_format(2.3*100, 50);
    

    229.99999999999997157829056959599256515502929687500000

    Edit: As the number of decimal places is varying (this also depends on the system used) the following might be useful - gets the full number for sure and removes trailing zeros:

    echo rtrim(number_format(1.0/3.432, 100),0);
    

    0.29137529137529138978379705804400146007537841796875

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题