-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomplete functions for exponentiation and roots.py
90 lines (82 loc) · 2.5 KB
/
complete functions for exponentiation and roots.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from random import *
#naming is now consistently in english
#fixed code for negative exponents
def exponentiation(base, exponent):
result = base
if exponent > 0:
for x in range(1,exponent):
result = result * base
elif exponent < 0:
for x in range(exponent,-1):
result = result * result
result = 1 / result
else:
result = 1
return(result)
#works with negative indexes now
def root(radicand, index, decimalDigits):
negIndex = False
if index == 0:
print("Error: not a valid exponent")
return
elif index < 0:
index *= -1
negIndex = True
elif radicand < 0:
print("Error: does not handle complex numbers")
return
border1 = 1
border2 = radicand
guess = 1
if radicand >= 1:
while True:
if border2 - border1 <= 1:
guess = uniform(border1, border2)
else:
guess = randint(border1, border2)
guess_p = exponentiation(guess, index)
if guess_p == radicand:
result = guess
break
elif border2 - border1 <= exponentiation(0.1, decimalDigits) * 0.1:
result = round(guess, decimalDigits)
break
elif guess_p > radicand:
border2 = guess
elif guess_p < radicand:
border1 = guess
else:
print("Error")
break
elif radicand < 1:
while True:
if border1 - border2 <= 1:
guess = uniform(border2, border1)
else:
guess = randint(border2, border1)
guess_p = exponentiation(guess, index)
if guess_p == radicand:
result = guess
break
elif border1 - border2 <= exponentiation(0.1, decimalDigits) * 0.1:
result = round(guess, decimalDigits)
break
elif guess_p > radicand:
border1 = guess
elif guess_p < radicand:
border2 = guess
else:
print("Error")
break
if negIndex == True:
result = 1 / result
return(round(result, decimalDigits))
# Test Values
#base = randint(0, 10)
#exponent = randint(-5, 5)
radicand = randint(0, 100)
index = randint(-5,5)
decimalDigits = 11
print(radicand, index)
print(root(radicand, index, decimalDigits))
#added the variable instead of the number