如何在 python 中存储大于 1*10^310 的浮点数?

How can I store float numbers larger than 1*10^310 in python?

我正在开发一个输出大矩阵条件数的程序,所以我使用幂法获得最大特征值,但这些值是大于 1*10^310 的大数(浮点数),最后值变成“无穷大”,我尝试了 decimal 模块,但它是一样的。我如何存储那些大的浮点值?或者可能是另一种使用较短值的方法? (我不允许使用任何显式帮助 Numpy 处理的模块)

如果可以的话,不要使用浮点值;它们很难推理并且会咬你!

每当您尝试使用浮点数,尤其是那些有很多数字的浮点数时,您应该考虑如何将其转换为整数范围,以及是否有超出值的浮点部分的无效或不必要的精度

  • 可能变成更大的 int,例如 10**40010**100000,这应该为您的浮点数字提供足够的空间,同时允许您使用整数 space
  • 直接转换或按比例缩小,丢弃小数点后的数字(考虑测量的准确度)
>>> int(1.0 * 10) * 10**999  # divide off 10**690 later or note in units
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>>> int(1.0 * 10**10)  # multiply by 10**300 later or note in units
10000000000

实际上,这就是您需要科学记数法的原因 - 如果您不需要数据,请不要存储所有数字,保留您需要的最小数量和大小因子的第二个乘数(科学表示法确实使用浮点数,但对于整数的想法是相同的)

然后,不用使用浮点数,您可以在完成数学运算后回忆最后的乘数(甚至分别将它们相乘)

甚至可能足以以某种规则的方式完全删除大部分数字,并在 post- 计算单位中显示因谁或其他什么消耗数据


虽然这个问题是关于大数的,但不幸的是,即使 decimal.Decimal 也不能像人们预期的那样处理小的浮点数,因为它们的存储方式会产生一些别名

https://en.wikipedia.org/wiki/Floating-point_arithmetic#IEEE_754:_floating_point_in_modern_computers

这对于普通的 python 浮动是有问题的,因此扩展到 Decimals,即使是您在正常使用中可能希望看到的大小!

>>> 9007199254740993.0
9007199254740992.0
>>> Decimal(9007199254740993.0)  # NOTE converted to float before Decimal
Decimal('9007199254740992')

改编自Which is the first integer that an IEEE 754 float is incapable of representing exactly?

原始问题的例子

>>> a = Decimal(10**310) * Decimal(1.0)
>>> b = Decimal(1)
>>> a + b - a
Decimal('0E+283')

更多示例

>>> a = Decimal(10**310)
>>> b = Decimal(0.1)
>>> a + b - a
Decimal('0')
>>> a
Decimal('10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')
>>> b
Decimal('0.1000000000000000055511151231257827021181583404541015625')
>>> 10**-100
1e-100
>>> Decimal(10**-100)
Decimal('1.00000000000000001999189980260288361964776078853415942018260300593659569925554346761767628861329298958274607481091185079852827053974965402226843604196126360835628314127871794272492894246908066589163059300043457860230145025079449986855914338755579873208034769049845635890960693359375E-100')
>>> 10**-1000
0.0
>>> Decimal(10**-1000)
Decimal('0')

您想使用 decimal 模块:

from decimal import Decimal

x = Decimal('1.345e1310')
y = Decimal('1.0e1310')
print(x + y)

结果:

2.345E+1310