Newbie: OverflowError: integer addition - Python data types??

Michael Tiomkin michael at camelot-it.com
Thu Feb 3 05:12:18 EST 2000


e.e.sutton at cummins.com wrote:

> I can't get a Python version of my VBScript Fibonacci series script to
> work.  I get an OverflowError: integer addition error at series 46.
>
> Can Python not handle large numbers?
>
> Is there a special data type I should be using?
>
> Series 45 is the following number.
> 45      1836311903 (1.84E+09)
>
> File "M:\SRC\scripting\fibonacci\fib.py", line 47, in Fibonacci
>     fib = fibA + fibB
> OverflowError: integer addition
>
> def Fibonacci(i):
>    fibA=1
>    fibB=1
>    if i == 0:
>       Fibonacci = fibA
>    elif i == 1:
>       Fibonacci = fibB
>    elif i > 1:
>       fib=0
>       for j in range(2, i+1):
>          fib = fibA + fibB
>          fibA = fibB
>          fibB = fib
>       Fibonacci = fib
>    return Fibonacci

  OK, your VBScript program just returns nonsense on n=46.  What about using floating point
numbers?  In this case you'll just get +Inf or -Inf in case of overflow.
-------------------------------------------
import Numeric
N=Numeric

b = (1.+N.sqrt(5))/2.
c = (1.-N.sqrt(5))/2.
e = (1-c)/(b-c)
f = (b-1)/(b-c)

def fibo(n):
  global b,c,e,f
  return e*b**n + f*c**n
----------------------------------
  The maximal value I could get on a PC is fibo(1474).
  BTW, your function tries to return itself for n<0!

  Michael




More information about the Python-list mailing list