Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Stuck on 'int too large to convert to float' error: How to solve it even after unsuccessfuly trying decimal module?

I’ve been trying to solve Lagrange’s sum using python (with numbers ranging from 0 to 2^1024) but it gives an overflow error when the input has a number of digits higher than 308 (just below 2^1024).

from random import shuffle
# p = a² + b² + c² + d² holds true for all whole numbers.

x = int(input('Write a whole number for the Lagrange Sum: '))

def lagrange_sum(p):
        num = a = b = c = d = 0           
        if num<p:
            a = int(pow((abs(p - b**2 - c**2 - d**2)), 1/2))
            b = int(pow((abs(p - a**2 - c**2 - d**2)), 1/2))
            c = int(pow((abs(p - a**2 - b**2 - d**2)), 1/2))
            d = int(pow((abs(p - a**2 - b**2 - c**2)), 1/2))
            p = []

            squared_values = [a, b, c, d]
            p.extend(squared_values)

            sum_evaluation = x == a**2 + b**2 + c**2 + d**2
            if sum_evaluation:
                print(p)
            else:
                shuffle(squared_values)
                print(squared_values)

if __name__ == '__main__':
    lagrange_sum(x)

I tried searching for a solution everywhere and I have spent huge amounts of hours trying to figure it out by myself, but in vain. How can I ‘increase’ python’s capacity to perform operations on huge numbers inside a square root? The error occurs when performing the operations on a, b, c and d. I don’t want just to perform operations on a single huge number, since the number is an input from the user, whom s/he can choose whatever number, being larger than or equal to 2^1024 if s/he desires to.

Error in question: "OverflowError: int too large to convert to float"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Floats in Python can’t store values greater than about 1.8e308, as you noticed. There is no way around that.

But you CAN use an integer-only square root method, such as the built-in math.isqrt function. It’s available in the Python standard library from 3.8.

You can also use the decimal module, which supports arbitrarily large numbers with arbitrary precision. They are presumably slower, but that might not matter. Since you don’t want the decimals this will likely be a worse solution than isqrt but I wanted to include it anyway.

from decimal import Decimal

a = Decimal(2**2000)
print(a.sqrt()) # works fine
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading