jukaukor

HighPrecisionSquareRoot.py

Jun 20th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. # high precision square root
  2. # Newton's method
  3. # Juhani Kaukoranta 20.6.2018
  4.  
  5. import math
  6. from decimal import *
  7. def Newton(x,precision) :
  8. '''high precision square root'''
  9.  
  10. getcontext().prec=precision
  11. x0 = Decimal(1)
  12. for n in range (0,70):
  13. x1 = (x0 + Decimal(x)/x0)/Decimal(2)
  14. x0 = x1
  15. return(x0)
  16.  
  17. a = input("Give number from which to get square root ")
  18. x = float(a)
  19. precision = int(input("How many digits precision, f.eq 100 "))
  20. print("sqrt(",a,") = ",Newton(x,precision))
Advertisement
Add Comment
Please, Sign In to add comment