gpt4 book ai didi

python - 如何解决 int 太长而无法转换为 float 错误?

转载 作者:行者123 更新时间:2023-12-01 03:23:38 26 4
gpt4 key购买 nike

我正在尝试确定 e 的连续数字中的前 10 位素数,因此想要将 e 计算为任意位数的小数点。这是我的代码,但是当我输入 1000 时,它无法将阶乘转换为 float ,因为它太大了:

import math

def calculate_e(n):
e_real=0.0
e_test=0.0
accurate=False
term=1
while accurate==False:
e_real=sum(1 / float(math.factorial(i)) for i in range(term))
if (e_real-e_test)<n:
accurate=True
else:
accurate=False
term+=1
e_test=e_real
return e_real

a=input("Enter accuracy: ")+1
b=10**(-a)
e=round(calculate_e(b),a)
print e

此外,我遇到的另一件事是在不四舍五入的情况下打印该位数。假设我想查看 e 的前 3 位数字,我希望它输出 271,而不是 272。我能想到的最好办法就是使其更精确 1 位小数,并将其四舍五入到一位小数。

最佳答案

阶乘太大并不重要。您之前有一个更重要的问题:

>>> import math
>>> for term in range(30):
e_real = sum(1 / float(math.factorial(i)) for i in range(term))
print '%2d %.60f' % (term, e_real)

0 0.000000000000000000000000000000000000000000000000000000000000
1 1.000000000000000000000000000000000000000000000000000000000000
2 2.000000000000000000000000000000000000000000000000000000000000
3 2.500000000000000000000000000000000000000000000000000000000000
4 2.666666666666666518636930049979127943515777587890625000000000
5 2.708333333333333037273860099958255887031555175781250000000000
6 2.716666666666666341001246109954081475734710693359375000000000
7 2.718055555555555447000415369984693825244903564453125000000000
8 2.718253968253968366752815200015902519226074218750000000000000
9 2.718278769841270037233016410027630627155303955078125000000000
10 2.718281525573192247691167722223326563835144042968750000000000
11 2.718281801146384513145903838449157774448394775390625000000000
12 2.718281826198492900914516212651506066322326660156250000000000
13 2.718281828286168710917536373017355799674987792968750000000000
14 2.718281828446759362805096316151320934295654296875000000000000
15 2.718281828458230187095523433526977896690368652343750000000000
16 2.718281828458994908714885241352021694183349609375000000000000
17 2.718281828459042870349549048114567995071411132812500000000000
18 2.718281828459045534884808148490265011787414550781250000000000
19 2.718281828459045534884808148490265011787414550781250000000000
20 2.718281828459045534884808148490265011787414550781250000000000
21 2.718281828459045534884808148490265011787414550781250000000000
22 2.718281828459045534884808148490265011787414550781250000000000
23 2.718281828459045534884808148490265011787414550781250000000000
24 2.718281828459045534884808148490265011787414550781250000000000
25 2.718281828459045534884808148490265011787414550781250000000000
26 2.718281828459045534884808148490265011787414550781250000000000
27 2.718281828459045534884808148490265011787414550781250000000000
28 2.718281828459045534884808148490265011787414550781250000000000
29 2.718281828459045534884808148490265011787414550781250000000000

这些是在e_real中计算出的精确值。您可以看到,当 term 变为 18 或更大时,您的 e_real 不再发生变化。 float 根本无法存储接近 1000 个有效数字。

正如 SethMMorton 所说,检查 decimal 模块。这可以解决您的两个问题。

关于python - 如何解决 int 太长而无法转换为 float 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41645554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com