如何对浮点输出执行单元测试? - Python [英] How to perform unittest for floating point outputs? - python

查看:117
本文介绍了如何对浮点输出执行单元测试? - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我正在为返回浮点数的函数编写单元测试,我可以按照我的机器的精度来做到这一点:

Let's say I am writing a unit test for a function that returns a floating point number, I can do it as such in full precision as per my machine:

>>> 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

在OS/发行版/机器上相同的全浮点精度是否相同?

Will the same full floating point precision be the same across OS/distro/machine?

我可以尝试四舍五入并进行这样的单元测试:

I could try to round off and do a unit test as such:

>>> class Testdiv(unittest.TestCase):
...     def testdiv(self):
...             assert round(div(1,9),4) == 0.1111
... 
>>>

我也可以使用log(output)进行断言,但是要保持固定的十进制精度,我仍然需要进行舍入或截断.

I could also do an assert with log(output) but to keep to a fix decimal precision, I would still need to do rounding or truncating.

但是一个人应该用什么其他方式用Python处理浮点输出的单元测试?

But what other way should one pythonically deal with unittesting for floating point output?

推荐答案

Python中float的精度取决于基础C表示形式.来自 教程/浮点算法:问题和局限性,15.1 :

The precision of float in Python is dependent on the underlying C representation. From Tutorial/Floating Point Arithmetic: Issues and Limitations, 15.1:

今天(2000年11月)几乎所有机器都使用IEEE-754浮点 算法,几乎所有平台都将Python浮点数映射到IEEE-754 双精度".

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.assertAmostEqual :


As for testing, a better idea is to use existing functionality, e.g. TestCase.assertAmostEqual:

assertAlmostEqual(第一,第二,地方= 7,msg =无,delta =无)

assertAlmostEqual(first, second, places=7, msg=None, delta=None)

测试第一第二是否近似(或不近似) 通过计算差值等于四舍五入到给定数 十进制位数(默认为7),然后与零进行比较.如果提供的是 delta 而不是 places ,则 first second 之间的差异必须小于或等于(或大于 delta .

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 +):

If you prefer to stick to assert statement, you could use the 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 .

The default relative tolerance of math.close is 1e-09, "which assures that the two values are the same within about 9 decimal digits.". For more information about math.close see PEP 485.

这篇关于如何对浮点输出执行单元测试? - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆