在下面的代码中,coeff1和coeff2都是Decimal对象。当我使用type(coeff1)检查它们的类型时,我得到了(类'decimal.Decimal'),但是当我进行测试代码并检查了十进制对象时,我得到了十进制。十进制,没有单词class
coeff1 = system[i].normal_vector.coordinates[i]
coeff2 = system[m].normal_vector.coordinates[i]
x = coeff2/coeff1
print(type(x))
system.xrow_add_to_row(x,i,m)
另一个问题是,当我将xrow_add_to_row函数的第一个输入更改为负x时:
system.xrow_add_to_row(-x,i,m)
我在更改的代码的上方行上收到无效的操作错误:
<ipython-input-11-ce84b250bafa> in compute_triangular_form(self)
93 coeff1 = system[i].normal_vector.coordinates[i]
94 coeff2 = system[m].normal_vector.coordinates[i]
---> 95 x = coeff2/coeff1
96 print(type(coeff1))
97 system.xrow_add_to_row(-x,i,m)
InvalidOperation: [<class 'decimal.DivisionUndefined'>]
但是然后在测试代码中我再次对Decimal对象使用负数,并且效果很好。知道可能是什么问题吗?谢谢。
答案 0 :(得分:3)
decimal.DivisionUndefined
。当除数为零(decimal.DivisionByZero
)时,您会得到一个不同的异常,这有点令人困惑
>>> import decimal.Decimal as D
>>> D(0) / D(0)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
D(0) / D(0)
decimal.InvalidOperation: [<class 'decimal.DivisionUndefined'>]
>>> D(1) / D(0)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
D(1) / D(0)
decimal.DivisionByZero: [<class 'decimal.DivisionByZero'>]