Dividing and multiplying Decimal objects in Python

后端 未结 1 523
逝去的感伤
逝去的感伤 2021-01-26 05:31

In the following code, both coeff1 and coeff2 are Decimal objects. When i check their type using type(coeff1), i get (class \'decimal.Decimal\') but when i made a test code and

相关标签:
1条回答
  • 2021-01-26 06:05

    decimal.DivisionUndefined is raised when you attempt to divide zero by zero. It's a bit confusing as you get a different exception when only the divisor is zero (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'>]
    
    0 讨论(0)
提交回复
热议问题