-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
257 lines (202 loc) · 8.01 KB
/
calculator.py
File metadata and controls
257 lines (202 loc) · 8.01 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
class AdvancedCalculator:
def __init__(self):
pass
def add (self, num1, num2):
return num1 + num2
def subtract(self, num1, num2):
return num1 - num2
def multiply(self, num1, num2):
return num1 * num2
def divide(self, num1, num2):
if num2 == 0:
return "Error: Cannot divide by zero. Please try again."
return num1 / num2
def truncate_n(self, x, n=7):
power = self.find_power(10, n)
num = x * power
num = self.my_round(num)
num /= power
return num
def my_round(self, x):
int_part = int(x)
frac_part = x - int_part
if frac_part >= 0.5:
return int_part + 1
else:
return int_part
def find_square_root(self, n):
"""
Find the square root of a non-negative number $n$.
"""
x = n
y = 1
e = 1e-16
helper = 0
while ((x-y) > e):
x = (x+y)/2
y = n / x
if helper == (x-y):
break
else:
helper = (x-y)
return x
def find_factorial(self, n):
# To calculate 36525! set sys.set_int_max_str_digits(150,789)
res = 1
if n == 1 or n == 0:
return 1
for i in range(1, n+1):
res = res * i
return res
def find_power(self, base, exponent) -> float:
res: float = 1.0
# Zero exponent
if exponent == 0:
return 1.0
# Handle negative exponents
if exponent < 0:
return 1 / self.find_power(base, -exponent)
int_part = int(exponent)
frac_part = exponent - int_part
# int exponent
for _ in range(int_part):
res *= base
# fractional exponent
if frac_part > 0:
current = base
power = 1.0
frac = frac_part
k = 1
while frac > 1e-10:
# print("before", current)
current = self.find_square_root(current)
# print("after", current)
twoK = self.find_n_th_root(2, k)
if frac >= 1 / twoK:
power *= current
frac -= 1 / twoK
k += 1
res *= power
return res
def calculate_simple_interest(self, principal, rate, time):
SI = (principal * rate * time) / 100
return SI
def calculate_compound_interest(self, principal, rate, time, periods):
r = rate / 100
A = principal * self.find_power(1+(r/periods), (periods * time))
CI = A - principal
return CI
def find_n_th_root(self, x, n):
"""
Newton-Raphson Method
"""
x0 = 3
a = x
i= 0
while True:
xn = (((n - 1) * x0) + (a/self.find_power(x0, n-1)))/n
if self.truncate_n(xn) == self.truncate_n(x0) or i > 20:
break
x0 = xn
i += 1
return x0
def compute_log_base_a_of_b(self, a, b) -> float:
res = 1.0
exponent = 0.0
power = [a, 1/2, 1/4, 1/8, 1/16, 1/32, 1/64, 1/128, 1/256]
for i in range(len(power)):
if i == 0:
p = a
else:
# print("else", i, power[i])
p = self.find_power(a, power[i])
while res * p < b:
exponent += p
res = res * p
# print(power[i], res)
return exponent
def calculate_sine(self, degrees):
"""Taylor Series method"""
pi = 3.14
res = 0
# convert degrees to radians
rad = degrees * pi/180
# Maclaurin Series Formula for sin(x)
for k in range(10):
res += self.find_power(-1, k) * self.find_power(rad, 2*k+1) / self.find_factorial(2*k+1)
return res
def start(self):
print("Welcome to the Advanced Calculator!")
while True:
print("Please select an operation:")
print("1: Add\n2: Subtract\n3: Multiply\n4: Divide\n5: Square root\n6: Factorial\n7: Power\n8: Simple Interest\n" \
"9: Compound Interest\n10: n_th Root\n11: log base a of b\n12: Sine\n13: Exit")
ch = int(input("Enter choice (1-13): "))
match ch:
case 1:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
res = self.add(num1, num2)
print(f"Result: {num1} + {num2} = {res}")
case 2:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
res = self.subtract(num1, num2)
print(f"Result: {num1} - {num2} = {res}")
case 3:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
res = self.multiply(num1, num2)
print(f"Result: {num1} * {num2} = {res}")
case 4:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
res = self.divide(num1, num2)
print(f"Result: {num1} / {num2} = {res}")
case 5:
n = float(input("Enter a number: "))
res = self.find_square_root(n)
print(f"square root of {n} is {res}")
case 6:
n = int(input("Enter a number: "))
res = self.find_factorial(n)
print(f"Factorial of {n} is {res}")
case 7:
base = float(input("enter the base: "))
exponent = float(input("enter exponent: "))
res = self.find_power(base, exponent)
print(f"The result of {base} ^ {exponent}: {res} ")
case 8:
principal = float(input("enter principal: "))
rate = float(input("enter rate: "))
time = float(input("enter time: "))
res = self.calculate_simple_interest()
print(f"The simple interest is: {res}")
case 9:
principal = float(input("enter principal: "))
rate = float(input("enter rate: "))
time = float(input("enter time: "))
period = float(input("enter period: "))
res = self.calculate_compound_interest(principal, rate, time, period)
print(f"The compoud interest is: {res}")
case 10:
x = float(input("enter a value: "))
n = float(input("enter n: "))
res = self.find_n_th_root(x, n)
print(f"result is: {res}")
case 11:
a = float(input("Enter base: "))
b = float(input("Enter value: "))
res = self.compute_log_base_a_of_b(a, b)
print(f"log base {a} of {b} = {res}")
case 12:
d = float(input("Enter degree: "))
res = self.calculate_sine(d)
print(f"sin of degree {d} is {res}")
case 13:
break
case _:
print("Enter a valid option")
print("__________________________\n\n")
calculator = AdvancedCalculator()
calculator.start()