我有一个返回浮点数的方法,比如1.234567890。我想测试一下它是否真的能这样做。但是,似乎这个返回的浮点数在不同的平台上具有不同的精度,所以我如何断言返回值为1.23456789?如果我这样做的话:
$this->assertEqual(1.23456789, $float);
然后,在一些精度不够的平台上,这可能会失败。
发布于 2009-12-26 18:32:44
为了获得更高的准确性,您可以考虑使用BCMath。
发布于 2013-01-18 22:03:54
到目前为止,还没有提到assertEquals支持通过offering a delta to specifiy precision比较浮点数
$this->assertEquals(1.23456789, $float, '', 0.0001);
感谢@Antoine87 for pointing out:since phpunit 7.5,你应该使用assertEqualsWithDelta()
$this->assertEqualsWithDelta(1.23456789, $float, 0.0001);
发布于 2019-04-17 23:04:50
作为@bernhard-wagner answer的更新,您现在应该使用assertEqualsWithDelta()
since phpunit 7.5。
$this->assertEqualsWithDelta(1.23456789, $float, 0.0001);
https://stackoverflow.com/questions/1963077
复制相似问题