我应该使用十进制库吗?

时间:2018-08-20 02:20:47

标签: python python-3.x

a = 1.5
i = 0
k = 0.2
results = []

while True:

    i += 1
    results.append(k)

    if k < a**i:
        continue

    elif k > a**i and k < 2 * a**1:
        k = k - a**i

    elif k > 2 * a**i:
        k = k - 2 * a**i

    else:
        break


print(results)

这是解决实际数学问题的python代码。当我运行它时,终端会给出这样的回溯:

Traceback (most recent call last):
  File "fusing.py", line 11, in <module>
    if k < a**i:
OverflowError: (34, 'Result too large')

我在堆栈溢出中发现another question关于该错误。接受的答案表明,OP可以使用十进制库对其进行修复。我不确定是应该使用它还是有其他解决方案。如果有人可以告诉我该怎么办,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

您处于无限循环中-k始终小于i> 1的幂的1.5。 因此,您一直在结果中添加项目(k),直到溢出为止。