Sab ಠ_ಠ
Sab ಠ_ಠ

Reputation: 135

Python3: long int too large to convert to float

I've been checking some topics around here about the same problem I'm getting but they don't seem to help.

My problem is when I try to execute the following code, I get the error found in the title. How do I go around this?

 d=2
 while(n != 1):        
        n = 2
        d = (math.sqrt(2 + d))
        n= (n/d)
        f = (f * (n))
 print (f)

Upvotes: 1

Views: 4682

Answers (1)

Ramchandra Apte
Ramchandra Apte

Reputation: 4079

That's because math.sqrt, as a consequence of using the C sqrt function, works on floating point number which are not unlimited in size. Python is unable to convert the long integer into a floating point number because it is to big. See this question on ways to square root large integers.

Better, you could use the decimal module, which is an unlimited size number type stored in base-10. Use decimal.Decimal(number).sqrt() to find the sqrt of a number.

Upvotes: 3

Related Questions