Overflowerror: Long Int Too Large to Convert to Float in Python

OverflowError: int too large to convert to float for p = 10**200-1 and q = -10**200

As the error points out, the int is too large to be converted to a float normally.
Python has this module named decimal, you could use decimal.Decimal to do your float conversions.

from decimal import Decimal

def ec_grad2(p, q):
delta = p ** 2 - 4 * q
sqrt_delta = Decimal(delta) ** Decimal(0.5)
if delta < 0:
return "No real solutions"
elif delta == 0:
x1 = -p / 2
x2 = x1
return (x1, x2)
elif delta > 0:
x1 = -2 * q / (p + sqrt_delta)
x2 = 2 * q / (sqrt_delta - p)
return (x1, x2)

def printing(function_return):
if type(function_return) == str:
print(function_return)
else:
a, b = function_return
print("x1 = {:.20f} si x2 = {:.20f}".format(a, b))

This should work for you

OverflowError: int too large to convert to float - Python

According to Python Tutorial in cases where precision is important it's better to use decimal or fractions modules.

For example, instead of writing f2 = 1/(exp+1) you should write

from fractions import Fraction
f2 = Fraction(1, exp+1)

Read this article to get a better understanding.

Note that doing such heavy computations is not recommended in Python itself even with built-in libraries like fractions. You should use libraries such as NumPy for better performance and also better precision.

Python OverflowError: int too large to convert to float

The best solution to this question runs in linear time. Here's the idea:

let's look at a shorter example: 1234

consider the total sum of the substrings which end on the ith digit (0..3)

substringsum[0]: 1 (we have only a single substring)
substringsum[1]: 2 + 12 (two substrings)
substringsum[2]: 3+23+123 (three substrings)
substringsum[3]: 4 + 34+234+1234

see a pattern?

let's look at substringsum[2]:

3 + 23 + 123 = 3 + 20+3 + 120+3 = 3*3 + 20+120 = 3*3 + 10*(2+12) = 3*3 +10*substringsum[1]

in general:

substringsum[k] = (k+1)*digit[k] + 10 * substringsum[k-1]

This you can compute in linear time.

This type of ideas is called "Dynamic Programming"

Python: long int too large to convert to float when calculating pi

Floats can only represent numbers up to sys.float_info.max, or 1.7976931348623157e+308. Once you have an int with more than 308 digits (or so), you are stuck. Your iteration fails when p1024 has 309 digits:

179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216L

You'll have to find a different algorithm for pi, one that doesn't require such large values.

Actually, you'll have to be careful with floats all around, since they are only approximations. If you modify your program to print the successive approximations of pi, it looks like this:

2.914213562373094923430016933707520365715026855468750000000000
3.140579250522168575088244324433617293834686279296875000000000
3.141592646213542838751209274050779640674591064453125000000000
3.141592653589794004176383168669417500495910644531250000000000
3.141592653589794004176383168669417500495910644531250000000000
3.141592653589794004176383168669417500495910644531250000000000
3.141592653589794004176383168669417500495910644531250000000000

In other words, after only 4 iterations, your approximation has stopped getting better. This is due to inaccuracies in the floats you are using, perhaps starting with 1/math.sqrt(2). Computing many digits of pi requires a very careful understanding of the numeric representation.



Related Topics



Leave a reply



Submit