From 76bc94d35517116c7210b4843f935646084697a5 Mon Sep 17 00:00:00 2001 From: Lavanya_Bhushan_Talele Date: Thu, 21 May 2026 00:48:08 +0530 Subject: [PATCH] feat: added basic trigonometry functions --- math/Simple-Calculator/Simple-Calculator.py | 78 ++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/math/Simple-Calculator/Simple-Calculator.py b/math/Simple-Calculator/Simple-Calculator.py index 83a67c8..599740c 100644 --- a/math/Simple-Calculator/Simple-Calculator.py +++ b/math/Simple-Calculator/Simple-Calculator.py @@ -1,3 +1,7 @@ +import math + +from matplotlib.pylab import angle + class Calculator: def __init__(self): pass @@ -13,11 +17,17 @@ def start_calculation(self): print('7. Power') print('8. Factorial') print('9. Square root') - print('10. Exit') + print('10. Sine') + print('11. Cosine') + print('12. Tangent') + print('13. Cotangent') + print('14. Secant') + print('15. Cosecant') + print('16. Exit') while True: try: - user_input = int(input('Enter your choice to calculate(1-10): ')) + user_input = int(input('Enter your choice to calculate(1-16): ')) if user_input == 1: self.addition() elif user_input == 2: @@ -37,6 +47,18 @@ def start_calculation(self): elif user_input == 9: self.sqrt() elif user_input == 10: + self.sine() + elif user_input == 11: + self.cosine() + elif user_input == 12: + self.tangent() + elif user_input == 13: + self.cotangent() + elif user_input == 14: + self.secant() + elif user_input == 15: + self.cosecant() + elif user_input == 16: print('Goodbye! Have a good day.') break else: @@ -49,6 +71,12 @@ def start_calculation(self): except Exception as e: print(f'Something went wrong: {e}') + def clean(self, x, eps=1e-10): + if abs(x) < eps: + return 0.0 + return x + + def addition(self): a = int(input('enter first value: ')) b = int(input('enter second value: ')) @@ -103,7 +131,53 @@ def sqrt(self): sqrt = num ** 0.5 print(f"Square root of {num} : {sqrt}") + def sine(self): + angle = float(input("Enter the angle in degrees: ")) + rads = math.radians(angle) + sin_val = self.clean(math.sin(rads)) + print(f"Sine of {angle} degrees is: {sin_val}") + + def cosine(self): + angle = float(input("Enter the angle in degrees: ")) + rads = math.radians(angle) + cos_val = self.clean(math.cos(rads)) + print(f"Cosine of {angle} degrees is: {cos_val}") + + def tangent(self): + angle = float(input("Enter the angle in degrees: ")) + rads = math.radians(angle) + tan_val = self.clean(math.tan(rads)) + print(f"Tangent of {angle} degrees is: {tan_val}") + + def cotangent(self): + angle = float(input("Enter the angle in degrees: ")) + rads = math.radians(angle) + tan_val = self.clean(math.tan(rads)) + if tan_val != 0: + cot_val = 1 / tan_val + print(f"Cotangent of {angle} degrees is: {cot_val}") + else: + print(f"Cotangent of {angle} degrees is undefined.") + + def secant(self): + angle = float(input("Enter the angle in degrees: ")) + rads = math.radians(angle) + cos_val = self.clean(math.cos(rads)) + if cos_val != 0: + sec_val = 1 / cos_val + print(f"Secant of {angle} degrees is: {sec_val}") + else: + print(f"Secant of {angle} degrees is undefined.") + def cosecant(self): + angle = float(input("Enter the angle in degrees: ")) + rads = math.radians(angle) + sin_val = self.clean(math.sin(rads)) + if sin_val != 0: + csc_val = 1 / sin_val + print(f"Cosecant of {angle} degrees is: {csc_val}") + else: + print(f"Cosecant of {angle} degrees is undefined.") cal = Calculator() cal.start_calculation()