测试人社区

求助:python-pytest实战1中,老师布置的乘法测试用例设计,Python中出现计算精度损失,请问这怎么解决

‘’’
代码:
class Calc:

def div(self, a, b):
    return a/b

def mul(self, a, b):
    return a*b

‘’’
class Testcalc:
def setup(self):
self.calc=Calc()
‘’’
我当时是把值写在了yaml文件中,举例传入a,b,c
分别是 0.77897,1.37893,1.0741451021
‘’’
‘’’ @pytest.mark.parametrize(“a,b,c”,yaml.safe_load(open("/Users/caohe/PycharmProjects/hogwarts_caohe/pytest1/source/miltcorrect.yml")))
def test_mul(self,a,b,c):
assert self.calc.mul(a,b) == c
‘’’
然后Python在运行后的预期结果是 1.0741451021000001


求助怎么修改?网上看了什么decimal方法,但是还是不太会。
‘’’

用这个库就可以了

>>> from decimal import Decimal
>>> Decimal('1.37893')*Decimal('0.77897')
Decimal('1.0741451021')

谢谢老师