gpt4 book ai didi

带小数的 PHP 和单元测试断言

转载 作者:IT王子 更新时间:2023-10-29 01:23:57 27 4
gpt4 key购买 nike

我有一个方法可以返回像 1.234567890 这样的 float 。我想测试它是否确实如此。但是,似乎这个返回的 float 在不同平台上具有不同的精度,那么我如何断言返回值为 1.23456789?如果我这样做:

$this->assertEqual(1.23456789, $float);

然后在某些精度不够的平台上可能会失败。

最佳答案

到目前为止还没有提到 assertEquals 支持 offering a delta to specifiy precision 比较 float :

$this->assertEquals(1.23456789, $float, '', 0.0001);

感谢@Antoine87 的 pointing out :从 phpunit 7.5 开始你应该使用 assertEqualsWithDelta():

$this->assertEqualsWithDelta(1.23456789, $float, 0.0001);

关于带小数的 PHP 和单元测试断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1963077/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com