-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorApplication.py
More file actions
37 lines (30 loc) · 1.25 KB
/
CalculatorApplication.py
File metadata and controls
37 lines (30 loc) · 1.25 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
class Calculator:
def __init__(self):
"""
Initialize the Calculator with a default precision for results.
:param precision: Number of decimal places for the output (default is 2).
"""
self.precision = 2
def format_result(self, result: float) -> float:
"""
Format the result to the specified precision.
:param result: The result of the operation.
:return: The formatted result.
"""
return round(result, self.precision)
def add(self, a: float, b: float) -> float:
return self.format_result(a + b)
def subtract(self, a: float, b: float) -> float:
return self.format_result(a - b)
def multiply(self, a: float, b: float) -> float:
return self.format_result(a * b)
def divide(self, a: float, b: float) -> float:
if b == 0:
raise ValueError("Division by zero is not allowed.")
return self.format_result(a / b)
def power(self, base: float, exponent: float) -> float:
return self.format_result(base ** exponent)
def square_root(self, a: float) -> float:
if a < 0:
raise ValueError("Cannot calculate the square root of a negative number.")
return self.format_result(a ** 0.5)