Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: OverflowError please help

  1. #1
    solartic's Avatar
    solartic is offline Learning Programmer
    Join Date
    Feb 2008
    Posts
    92
    Rep Power
    15

    OverflowError please help

    hi all
    im trying to create a function that identifies if a number is a Fibonacci numbers base on:
    Fibonacci number - Wikipedia, the free encyclopedia
    here is what i did
    Code:
    import math
    
    gratio = (1 + math.sqrt(5))/2#golden ratio
    lim = 10**999 #math.log(x[, base])
    
    def isFibon(x):#test if number is a Fibonacci numbers
    	if (math.log(math.sqrt(5*x),gratio) + 0.5) == x:
    		return 1
    	else:
    		return 0
    		
    for n in range(20):
    	if isFibon(n) == 1:
    		print n
    and hear id what i got

    Traceback (most recent call last):
    File "untitled.py", line 15, in <module>
    if isFibon(n) == 1:
    File "untitled.py", line 9, in isFibon
    if (math.log(math.sqrt(5*x),gratio) + 0.5) == x:
    OverflowError: math range error

    im planning on using another method but im still curious what all this mean
    any help will be appreciated

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: OverflowError please help

    I'm guessing that means that your function generated a number way too big for python to support. Honestly I'm not sure, but I believe that is what that means.

    How about a recursive method?

    Code:
    function fib(int n) {
        if (n == 1) return 1;
        return fib(n-1) + fib(n-2);
    }
    ?

  4. #3
    solartic's Avatar
    solartic is offline Learning Programmer
    Join Date
    Feb 2008
    Posts
    92
    Rep Power
    15

    Re: OverflowError please help

    that sounds like it, i guess i forgot even python has a limit with how big its values can be.
    thanks the recursive method would work but i had already resorted to a simple loop method
    Thanks

  5. #4
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: OverflowError please help

    You're welcome!

  6. #5
    yamman13 is offline Learning Programmer
    Join Date
    Dec 2008
    Posts
    56
    Rep Power
    0

    Re: OverflowError please help

    google "how to think like a computer scientist, learning with python". Its a free set of tutorials to python.

    it first introduces this problem very early on, then comes back to it much later in the chapter on lists I think.

  7. #6
    solartic's Avatar
    solartic is offline Learning Programmer
    Join Date
    Feb 2008
    Posts
    92
    Rep Power
    15

    Re: OverflowError please help

    thanks ill look it up

  8. #7
    PythonPower's Avatar
    PythonPower is offline Programming Professional
    Join Date
    Feb 2009
    Posts
    228
    Rep Power
    13

    Re: OverflowError please help

    You could use Decimals instead. They can deal with, theoretically, any finite values and by default work to about 18 significant figures. They are slower than floats though...

    I can't paste the link to the Python docs on Decimals since I haven't made 10 posts yet, but Googling for "python decimal" should get you close.

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: OverflowError please help


    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    PythonPower's Avatar
    PythonPower is offline Programming Professional
    Join Date
    Feb 2009
    Posts
    228
    Rep Power
    13

    Re: OverflowError please help

    Yes, the second link was what I had in mind.

  11. #10
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: OverflowError please help

    I got rid of the first one to avoid confusion.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts