Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # high precision square root
- # Newton's method
- # Juhani Kaukoranta 11.11.2018
- import math
- from decimal import *
- def Newton(x,precision) :
- '''high precision square root'''
- if x > 0 :
- getcontext().prec=precision
- x0 = Decimal(1)
- for n in range (0,70):
- x1 = (x0 + Decimal(x)/x0)/Decimal(2)
- x0 = x1
- return(x0)
- if x == 0 :
- return(0)
- if x < 0 :
- print("Only non-negative number has a square root")
- a = input("Give non-negative number from which to get square root ")
- x = float(a)
- if x >= 0 :
- precision = int(input("How many digits precision, f.eq 100 "))
- print("sqrt(",a,") = ",Newton(x,precision))
- else:
- print("Only non-negative number has square root")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement