谁能告诉我为什么超过 27 位小数会导致 TypeError?我查看了官方文档,但没有看到答案。错误消息提到浮动,但我在代码中看不到任何浮动。我特别摆脱了所有的花车。
Evaluate number e to n significant digits (up to 27): 28
Traceback (most recent call last):
File "ex6.py", line 20, in <module>
while (deci.Decimal(1) / deci.Decimal((math.factorial(step)))) > deci.Decimal(eval(sigdig2)):
File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'Decimal' and 'float'
>
# imports necessary modules
import math
import decimal as deci
# defines variables
eul = 0
step = 0
# queries for input
sigdig = int(raw_input("Evaluate number e to n significant digits (up to 27): "))
sigdig2 = 'deci.Decimal(1)/' + str(deci.Decimal(10) ** deci.Decimal(sigdig))
deci.getcontext().prec = (sigdig + 1)
# evaluates e
while (deci.Decimal(1) / deci.Decimal((math.factorial(step)))) > deci.Decimal(eval(sigdig2)):
eul += (deci.Decimal(1) / deci.Decimal((math.factorial(step))))
step +=1
#prints the result
print
printer = '"e = %.' + str(sigdig) + 'f" % (eul)'
print eval(printer)