Abhlash commited on
Commit
406da8a
·
verified ·
1 Parent(s): 4493d75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -11
app.py CHANGED
@@ -19,18 +19,55 @@ def read_root():
19
  def calculate(request: CalculationRequest):
20
  a = request.a
21
  b = request.b
22
- operation = request.operation
23
 
24
- if operation == "add":
25
- result = a + b
26
- elif operation == "subtract":
27
- result = a - b
28
- elif operation == "multiply":
29
- result = a * b
30
- elif operation == "divide":
31
- result = a / b
32
- else:
33
- return {"error": "Invalid operation"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  return {"result": result}
36
 
 
19
  def calculate(request: CalculationRequest):
20
  a = request.a
21
  b = request.b
22
+ operation = request.operation.lower()
23
 
24
+ try:
25
+ if operation == "add":
26
+ result = a + b
27
+ elif operation == "subtract":
28
+ result = a - b
29
+ elif operation == "multiply":
30
+ result = a * b
31
+ elif operation == "divide":
32
+ if b == 0:
33
+ raise HTTPException(status_code=400, detail="Division by zero is not allowed")
34
+ result = a / b
35
+ elif operation == "power":
36
+ result = math.pow(a, b)
37
+ elif operation == "modulus":
38
+ result = a % b
39
+ elif operation == "sqrt":
40
+ if a < 0:
41
+ raise HTTPException(status_code=400, detail="Square root of negative number is not allowed")
42
+ result = math.sqrt(a)
43
+ elif operation == "log":
44
+ if a <= 0:
45
+ raise HTTPException(status_code=400, detail="Logarithm of non-positive number is not allowed")
46
+ result = math.log(a)
47
+ elif operation == "log10":
48
+ if a <= 0:
49
+ raise HTTPException(status_code=400, detail="Logarithm of non-positive number is not allowed")
50
+ result = math.log10(a)
51
+ elif operation == "sin":
52
+ result = math.sin(math.radians(a))
53
+ elif operation == "cos":
54
+ result = math.cos(math.radians(a))
55
+ elif operation == "tan":
56
+ result = math.tan(math.radians(a))
57
+ elif operation == "asin":
58
+ result = math.degrees(math.asin(a))
59
+ elif operation == "acos":
60
+ result = math.degrees(math.acos(a))
61
+ elif operation == "atan":
62
+ result = math.degrees(math.atan(a))
63
+ elif operation == "factorial":
64
+ if a < 0 or not float(a).is_integer():
65
+ raise HTTPException(status_code=400, detail="Factorial of negative or non-integer number is not allowed")
66
+ result = math.factorial(int(a))
67
+ else:
68
+ raise HTTPException(status_code=400, detail="Invalid operation")
69
+ except Exception as e:
70
+ raise HTTPException(status_code=400, detail=str(e))
71
 
72
  return {"result": result}
73