1

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)
Sab ಠ_ಠ
  • 135
  • 1
  • 4
  • 13
  • possible duplicate of [OverflowError: long int too large to convert to float in python](http://stackoverflow.com/questions/16174399/overflowerror-long-int-too-large-to-convert-to-float-in-python) – Alex Mar 09 '14 at 06:15
  • If you execute that code, you get "NameError: name 'n' is not defined". What code are you actually executing? – Mark Dickinson Mar 09 '14 at 08:10
  • Oops. I missed the value of n=2 above d=2. – Sab ಠ_ಠ Mar 09 '14 at 08:31

1 Answers1

3

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.

Ramchandra Apte
  • 4,033
  • 2
  • 24
  • 44