Newbie: OverflowError: integer addition - Python data types??
Ivan Van Laningham
ivanlan at callware.com
Wed Feb 2 18:44:44 CET 2000
Hi All--
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
>
Aahz pointed out in another post that you need to use long integers.
The simplest change you can make is to change
fibA=1
fibB=1
to
fibA=1L
fibB=1L
This will cure your immediate problem.
However, two things.
1) If you're going to post code, post complete code. I.e., here you
forgot to post the line in which you called Fibonacci().
2) Defining a function Fibonacci() which uses a local variable inside
itself name Fibonacci is terrible practice. It's confusing and obscures
your meaning. When people read the function, every time they see the
name of a function inside the function, they're *first* going to think
"Recursion!" They have to go back and re-read it before they realize
it's only a variable having local scope. Also, in such a short function
there is no need to be so verbose, either; try renaming the Fibonacci
variable to f. I think you'll find it's much clearer.
<depend-on-the-kindness-of-strangers-and-someone-will-clarify-your-problem>-ly
y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan at callware.com
ivanlan at home.com
http://www.pauahtun.org
See also:
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
----------------------------------------------
More information about the Python-list
mailing list