如何使用pytest近似小数点处的值?

2024-04-19 06:43:57 发布

您现在位置:Python中文网/ 问答频道 /正文

查看documentation时,它表示默认情况下approx方法计算一个数字的小数点后3位,但是当我比较这个值时,它返回False

pytest.approx(6.495) == 6.49  # False

如何检查一个数字与另一个数字之间的小数位数是否接近


Tags: 方法falsepytestdocumentation情况数字小数点小数位
1条回答
网友
1楼 · 发布于 2024-04-19 06:43:57

它并没有说默认值在小数点后三位以内,而是说它在1e-6-或0.000001之内

By default, approx considers numbers within a relative tolerance of 1e-6 (i.e. one part in a million) of its expected value to be equal. This treatment would lead to surprising results if the expected value was 0.0, because nothing but 0.0 itself is relatively close to 0.0. To handle this case less surprisingly, approx also considers numbers within an absolute tolerance of 1e-12 of its expected value to be equal.

^ {}参数可以用来改变所考虑的大致相同的值:

>>> pytest.approx(6.495, rel=1e-3) == 6.49
True

相关问题 更多 >