-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathsqrt.py
37 lines (30 loc) · 852 Bytes
/
sqrt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Author: Jin Zhe
def input_val(message):
return float(input(message))
def middle(n1, n2):
return (n1 + n2)/2
def sqrt(n, error=1e-5):
if n < 0:
negError = ValueError('Input should be positive')
raise myError
if n < 1: # if n is smaller than 1, range to search is within [n, 1]
lo = n
hi = 1
else: # if n is greater than 1, range to search is within [0, n]
lo = 0
hi = n
mid = middle(lo, hi)
while(abs(mid**2 - n) > error):
if (mid**2 > n):
hi = mid
else:
lo = mid
mid = middle(lo, hi) # Update new mid
return mid
def main():
message = "Input float to carry out square root. Use a negative number to terminate."
val = input_val(message)
while val > 0:
print("Float of {} is {}".format(val, sqrt(val)))
val = input_val(message)
if __name__ == '__main__': main()