Python 数学范围错误
Python math range error
我在 Python 中尝试计算一个非常大的数字时遇到错误。这是我的代码:
# Where fourthNumber = 2790
# and dee = 413
emm = math.pow(fourthNumber, dee)
我的错误是:
line 44, in <module>
emm = math.pow(fourthNumber, dee)
OverflowError: math range error
有没有办法解决这个错误?我以为 Python 可以处理任意大的数字?还是我错了?任何帮助表示赞赏。谢谢!
内置函数 pow
适用于整数运算:
>>> pow.__module__
'__builtin__'
>>> pow is math.pow
False
>>> pow(2790, 413)
108276934...
问题是 math.pow(..)
适用于浮点数 。在 Python 中,浮点数不是任意大的 。只有 int
是(在 python-3.x, and long
s in python-2.x 中)。
然而,如果两个数字是整数,您可以使用 **
运算符,它执行 整数幂 (当然,参数是整数):
>>> 2790**413
10827693458027068918752254513689369927451498632867702850871449492721716762882046359646654407147290095143376244612860740505063304616869045757879636651922242895944635094287526023557872050108996014618928707382416906723717536207944990935946477343103732942220495426003253324856391048675505527041527544249845903325107575822015010197006079682477544271998209608154757421132764034059289159228295810448568286783859864141487725512980856505994152145510660350938086763233208252511256291934375881870590480237727775536326670654123168787472077359939510018827829233028430183558108518520524567765780717109616748933630364200317687291046055118737587697510939517252245710306646155772831436013971724481443654932630319085588147436112198934867224850036968074130558127066188475740553149587714112808551835880666012903651859580234129805580074844684526620091506655345299434455806896837926335229779632528684030400890708579038639280240022309690038032176604539091205540422068492362106868171343650410145963283813864374487990607671475570427243900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
如果您 将其转换为 float
,您将得到:
>>> float(2790**413)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: int too large to convert to float
所以错误清楚地表明 python 不能像 float
那样处理这么大的数字。
你可以处理任意大的整数数; math.pow
浮点数运算。
2790 ** 413 > 1000 ** 413 = 1e+1239,远高于 1e+308 左右的浮点数范围。
使用**
留在整数域内,得到你的大整数。
数学库中可能存在错误 - 此外,math.pow 有点多余,您可以用 fourthNumber ** dee
代替
威廉,**不同于math.pow
Unlike the built-in ** operator, math.pow() converts both its arguments to type float. Use ** or the built-in pow() function for computing exact integer powers.
如果你必须操作大数字,你也可以使用 numpy。
我在 Python 中尝试计算一个非常大的数字时遇到错误。这是我的代码:
# Where fourthNumber = 2790
# and dee = 413
emm = math.pow(fourthNumber, dee)
我的错误是:
line 44, in <module>
emm = math.pow(fourthNumber, dee)
OverflowError: math range error
有没有办法解决这个错误?我以为 Python 可以处理任意大的数字?还是我错了?任何帮助表示赞赏。谢谢!
内置函数 pow
适用于整数运算:
>>> pow.__module__
'__builtin__'
>>> pow is math.pow
False
>>> pow(2790, 413)
108276934...
问题是 math.pow(..)
适用于浮点数 。在 Python 中,浮点数不是任意大的 。只有 int
是(在 python-3.x, and long
s in python-2.x 中)。
然而,如果两个数字是整数,您可以使用 **
运算符,它执行 整数幂 (当然,参数是整数):
>>> 2790**413
10827693458027068918752254513689369927451498632867702850871449492721716762882046359646654407147290095143376244612860740505063304616869045757879636651922242895944635094287526023557872050108996014618928707382416906723717536207944990935946477343103732942220495426003253324856391048675505527041527544249845903325107575822015010197006079682477544271998209608154757421132764034059289159228295810448568286783859864141487725512980856505994152145510660350938086763233208252511256291934375881870590480237727775536326670654123168787472077359939510018827829233028430183558108518520524567765780717109616748933630364200317687291046055118737587697510939517252245710306646155772831436013971724481443654932630319085588147436112198934867224850036968074130558127066188475740553149587714112808551835880666012903651859580234129805580074844684526620091506655345299434455806896837926335229779632528684030400890708579038639280240022309690038032176604539091205540422068492362106868171343650410145963283813864374487990607671475570427243900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
如果您 将其转换为 float
,您将得到:
>>> float(2790**413)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: int too large to convert to float
所以错误清楚地表明 python 不能像 float
那样处理这么大的数字。
你可以处理任意大的整数数; math.pow
浮点数运算。
2790 ** 413 > 1000 ** 413 = 1e+1239,远高于 1e+308 左右的浮点数范围。
使用**
留在整数域内,得到你的大整数。
数学库中可能存在错误 - 此外,math.pow 有点多余,您可以用 fourthNumber ** dee
代替
威廉,**不同于math.pow
Unlike the built-in ** operator, math.pow() converts both its arguments to type float. Use ** or the built-in pow() function for computing exact integer powers.
如果你必须操作大数字,你也可以使用 numpy。