Jump to content

OverflowError please help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
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

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
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
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?

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

?

#3
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
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

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
You're welcome! :D

#5
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
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.

#6
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
thanks ill look it up

#7
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts
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.

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
decimal รข€” Decimal fixed point and floating point arithmetic — Python v2.6.1 documentation
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts
Yes, the second link was what I had in mind. ;)

#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I got rid of the first one to avoid confusion. :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#11
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
thanks guys ill try it out as soon as i get some time