Overflow Error Python Result Too Large
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error 34, Result too large up vote 0 down vote favorite I am trying to print the Fibonacci sequence and it always returns an Overflow error after about the 600th term. def fib(): import math from math import sqrt print "\nFibonacci Sequence up to the term of what?" n=raw_input(prompt) if n.isdigit(): if int(n)==0: return 0 elif int(n)==1: return 1 else: n_count=2 print "\n0\n1" while n_count
& solutions from a community of 418,616 IT Pros & Developers. It's quick & easy. Overflow error P: n/a Jane Austine >>> from math import e e**709 8.218407461554662e+307 e**710 Traceback (most recent call last): File "
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings http://stackoverflow.com/questions/16174399/overflowerror-long-int-too-large-to-convert-to-float-in-python and policies of this site About Us Learn more about Stack Overflow http://stackoverflow.com/questions/27946595/how-to-manage-division-of-huge-numbers-in-python the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only overflow error takes a minute: Sign up OverflowError: long int too large to convert to float in python up vote 10 down vote favorite 1 I tried to calculate poisson distribution in python as below: p = math.pow(3,idx) depart = math.exp(-3) * p depart = depart / math.factorial(idx) idx ranges from 0 But I got OverflowError: long int too large to convert overflow error python to float I tried to convert depart to float but no results. python share|improve this question edited Apr 23 '13 at 16:23 Sam Bruns 3,1772734 asked Apr 23 '13 at 16:20 user2312186 50116 Factorial gets really large, really fast –Hunter McMillen Apr 23 '13 at 16:22 1 What is the value of idx when you hit this error? –Pyrce Apr 23 '13 at 16:23 When you wish to compute a factorial, compute it's logarithm instead –zehelvion Jun 13 '15 at 13:10 add a comment| 4 Answers 4 active oldest votes up vote 15 down vote Factorials get large real fast: >>> math.factorial(170) 7257415615307998967396728211129263114716991681296451376543577798900561843401706157852350749242617459511490991237838520776666022565442753025328900773207510902400430280058295603966612599658257104398558294257568966313439612262571094946806711205568880457193340212661452800000000000000000000000000000000000000000L Note the L; the factorial of 170 is still convertable to a float: >>> float(math.factorial(170)) 7.257415615307999e+306 but the next factorial is too large: >>> float(math.factorial(171)) Traceback (most recent call last): File "
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to manage division of huge numbers in Python? up vote 2 down vote favorite I have a 100 digit number and I am trying to put all the digits of the number into a list, so that I can perform operations on them. To do this, I am using the following code: for x in range (0, 1000): list[x] = number % 10 number = number / 10 But the problem I am facing is that I am getting an overflow error something like too large number float/integer. I even tried using following alternative number = int (number / 10) How can I divide this huge number with the result back in integer type, that is no floats? python division share|improve this question edited Jul 19 at 15:53 ajcr 38.5k126790 asked Jan 14 '15 at 15:32 TorontoSummers 175213 add a comment| 4 Answers 4 active oldest votes up vote 16 down vote accepted In Python 3, number / 10 will return try to return a float. However, floating point values can't be of arbitrarily large size in Python and if number is large an OverflowError will be raised. You can find the maximum that Python floating point values can take on your system using the sys module: >>> import sys >>> sys.float_info.max 1.7976931348623157e+308 To get around this limitation, instead use // to get an integer back from the division of the two integers: number // 10 This will return the int floor value of number / 10 (it does not produce a float). Unlike floats, int values can be as large as you need them to be in Python 3 (within memory limits). You can now divide the large numbers. For instance, in Python 3: >>> 2**3000 / 10 OverflowError: integer division result too large for a float >>> 2**3000 // 10 123023192216111717693155881327... share|improve this answer edited Jan 14 '15 at 18:04 answered Jan 14 '15 at 15:38 ajcr 38.5k126790 1 But does this works on long int also? I suppose in case of a large number, python will automatically choose it as long it? –TorontoSummers Jan 14 '15 at 15:41 In Python 3 there is no long type (integers can be any size). It works in Python 2 with both types. –ajcr Jan 14 '