6

I'm getting 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)

An my error is:

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

Is there a way around this error? I thought Python could handle arbitrarily large numbers? Or am I wrong? Any help is appreciated. Thanks!

Coder117
  • 801
  • 2
  • 9
  • 22

5 Answers5

7

The problem is that math.pow(..) works on floating point numbers. In Python floating point numbers are not arbitrary large. Only ints are (in , and longs in ).

You can however use the ** operator which does integer power (given of course the arguments are integers) if the two numbers are integers:

>>> 2790**413
10827693458027068918752254513689369927451498632867702850871449492721716762882046359646654407147290095143376244612860740505063304616869045757879636651922242895944635094287526023557872050108996014618928707382416906723717536207944990935946477343103732942220495426003253324856391048675505527041527544249845903325107575822015010197006079682477544271998209608154757421132764034059289159228295810448568286783859864141487725512980856505994152145510660350938086763233208252511256291934375881870590480237727775536326670654123168787472077359939510018827829233028430183558108518520524567765780717109616748933630364200317687291046055118737587697510939517252245710306646155772831436013971724481443654932630319085588147436112198934867224850036968074130558127066188475740553149587714112808551835880666012903651859580234129805580074844684526620091506655345299434455806896837926335229779632528684030400890708579038639280240022309690038032176604539091205540422068492362106868171343650410145963283813864374487990607671475570427243900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

If you however cast it to a float, you get:

>>> float(2790**413)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: int too large to convert to float

So the error clearly shows that cannot handle this large numbers as floats.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
5

You can handle arbitrarily large integer numbers; math.pow operates of floating-point numbers.

2790 ** 413 > 1000 ** 413 = 1e+1239, way above the floating-point range of around 1e+308.

Use ** to stay within integers domain, and get your huge integer number.

9000
  • 39,899
  • 9
  • 66
  • 104
3

The builtin pow works with integer arithmetic:

>>> pow.__module__
'__builtin__'
>>> pow is math.pow
False
>>> pow(2790, 413)
108276934...
wim
  • 338,267
  • 99
  • 616
  • 750
0

Willem, ** is different from 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.

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

  • in what way is that not what I said... The answer says "`math.pow` works on floats." So of course that means it first *converts* it to floats... – Willem Van Onsem Mar 31 '17 at 20:20
  • afaik numpy works with 64-bit numbers, `np.power(2790,413)` warns that overflow occured and returns `-9223372036854775808`, which is incorrect. – Willem Van Onsem Mar 31 '17 at 20:27
  • Sorry, I misunderstood what you said :) –  Mar 31 '17 at 20:28
  • "You can also use numpy if you have to manipulate huge numbers." <- This isn't true. On some (but not all) platforms, NumPy has some support for the 80-bit IEEE 754-1985 extended precision type, but that's about as far as it goes. – Mark Dickinson Apr 01 '17 at 18:10
-1

There might be a bug in the math library - besides, math.pow is kind of redundant, you can do fourthNumber ** dee instead