Python math range error

I get an error when trying to calculate a very large number in Python. Here is my code:

# Where fourthNumber = 2790
# and dee = 413
emm = math.pow(fourthNumber, dee)

My mistake:

line 44, in <module>
    emm = math.pow(fourthNumber, dee)
OverflowError: math range error

Is there a way for this error? I thought Python could handle arbitrarily large numbers? Or I'm wrong? Any help is appreciated. Thank!

+4
source share
5 answers

You can handle arbitrarily large integers; math.powworks with floating point numbers.

2790 ** 413> 1000 ** 413 = 1e + 1239, the path above the floating point range is about 1e + 308.

Use **to stay within entire domains and get your huge integer number.

+4

, math.pow(..) . Python . int ( , long ).

**, ( , ), :

>>> 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

, , float s.

+4

pow :

>>> pow.__module__
'__builtin__'
>>> pow is math.pow
False
>>> pow(2790, 413)
108276934...
+3

, ** math.pow

Unlike the built-in ** operator, math.pow () converts both arguments to a float type. Use ** or the built-in pow () function to calculate exact integer powers.

You can also use numpy if you need to manipulate huge numbers.

0
source

There may be a mistake in the math library - in addition, math.pow is redundant, you can do fourthNumber ** deeinstead

-1
source

Source: https://habr.com/ru/post/1673745/


All Articles