我想在Python中计算(⌊2^(1918)*π⌋+124476),但是使用以下代码时出现了错误:
b = math.floor((2**1918) * math.pi) + 124476
print(b)
溢出错误:整数太大,无法转换为浮点数
如何使其正常工作?最终我只想将其全部转换为十六进制(如果这有助于回答我的问题),但我实际上只是想先将其转换为整数 :)
b = math.floor((2**1918) * math.pi) + 124476
print(b)
溢出错误:整数太大,无法转换为浮点数
如何使其正常工作?最终我只想将其全部转换为十六进制(如果这有助于回答我的问题),但我实际上只是想先将其转换为整数 :)
为了计算所需的结果,应使用任意精度计算技术。您可以自己实现算法或使用其中一个可用的库。
假设您正在寻找表达式的整数部分,需要大约600个十进制位才能精确存储结果。以下是使用mpmath获取它的方法:
最初的回答:
from mpmath import mp
mp.dps = 600
print(mp.floor(mp.power(2, 1918)*mp.pi + 124476))
2**1918
在十进制中包含578个有效数字,比你的IEEE754硬件能表示的最大浮点数还要大得多。因此,调用失败了。mpmath
模块。它专门用于浮点运算,超出了普通硬件处理的范围。m
和n
是整数,floor(n.something + m)
等于floor(n.something) + m
。所以在这种情况下,您要找的是floor(2 ** 1918 * pi)
加上一个整数(即124476)。floor(2 ** whatever * pi)
只是pi的前whatever + 2
位。因此,只需查找pi的前1920位,添加124476的位,然后输出十六进制数字。(2**1918) * math.pi
试图将整数转换为64位浮点精度,这是不够大的。您可以将math.pi
转换为fraction以使用任意精度。>>> math.floor((2**1918) * fractions.Fraction(math.pi) + 124476)
74590163000744212756918704280961225881025315246628098737524697383138220762542289800871336766911957454080350508173317171375032226685669280397906783245438534131599390699781017605377332298669863169044574050694427882869191541933796848577277592163846082732344724959222075452985644173036076895843129191378853006780204194590286508603564336292806628948212533561286572102730834409985441874735976583720122784469168008083076285020654725577288682595262788418426186598550864392013191287665258445673204426746083965447956681216069719524525240073122409298640817341016286940008045020172328756796
math.pi
仅以64位浮点精度定义。如果需要确切的值,请使用外部库,例如 mpmath
。hex
或字符串格式:>>> hex(math.floor((2**1918) * fractions.Fraction(math.pi) + 124476))
'0xc90fdaa22168c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001e63c'
>>> '%x' % math.floor((2**1918) * fractions.Fraction(math.pi) + 124476)
'c90fdaa22168c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001e63c'
>>> f'{math.floor((2**1918) * fractions.Fraction(math.pi) + 124476):X}'
'C90FDAA22168C0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001E63C'
x
提供小写十六进制,而 X
则提供大写十六进制。