假设我正在为一个返回 float 的函数编写单元测试,我可以按照我的机器完全精确地进行测试:
>>> import unittest
>>> def div(x,y): return x/float(y)
...
>>>
>>> class Testdiv(unittest.TestCase):
... def testdiv(self):
... assert div(1,9) == 0.1111111111111111
...
>>> unittest.main()
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
相同的完整浮点精度在操作系统/发行版/机器上是否相同?
我可以尝试四舍五入并进行这样的单元测试:
>>> class Testdiv(unittest.TestCase):
... def testdiv(self):
... assert round(div(1,9),4) == 0.1111
...
>>>
我也可以使用 log(output)
进行断言,但为了保持固定的小数精度,我仍然需要进行舍入或截断。
但是,还有什么其他方法可以用 Python 方式处理浮点输出的单元测试呢?
最佳答案
Python 中float
的精度取决于底层的C 表示。来自 Tutorial/Floating Point Arithmetic: Issues and Limitations, 15.1 :
Almost all machines today (November 2000) use IEEE-754 floating point arithmetic, and almost all platforms map Python floats to IEEE-754 “double precision”.
至于测试,更好的想法是使用现有功能,例如TestCase.assertAlmostEqual
:
assertAlmostEqual(first, second, places=7, msg=None, delta=None)
Test that first and second are approximately (or not approximately) equal by computing the difference, rounding to the given number of decimal places (default 7), and comparing to zero. If delta is supplied instead of places then the difference between first and second must be less or equal to (or greater than) delta.
例子:
import unittest
def div(x, y): return x / float(y)
class Testdiv(unittest.TestCase):
def testdiv(self):
self.assertAlmostEqual(div(1, 9), 0.1111111111111111)
self.assertAlmostEqual(div(1, 9), 0.1111, places=4)
unittest.main() # OK
如果您更喜欢使用 assert
语句,您可以使用 math.isclose
(Python 3.5+):
import unittest, math
def div(x, y): return x / float(y)
class Testdiv(unittest.TestCase):
def testdiv(self):
assert math.isclose(div(1, 9), 0.1111111111111111)
unittest.main() # OK
math.close
的默认相对公差是 1e-09,“这确保两个值在大约 9 个小数位内相同。”。有关 math.close
的更多信息,请参阅 PEP 485 .
关于python - 如何对浮点输出执行单元测试? - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33199548/