Laiba-Huggingface commited on
Commit
8c28bb8
Β·
verified Β·
1 Parent(s): 09c7fc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -4,8 +4,8 @@ import math
4
  def main():
5
  st.title("Ultimate Scientific Calculator")
6
 
7
- # Allow user to choose between degrees and radians
8
- use_degrees = st.toggle("Use Degrees for Trigonometric Functions?", value=True)
9
 
10
  # Display supported operations
11
  st.markdown("""
@@ -20,12 +20,14 @@ def main():
20
  - **Absolute Value (`abs(x)`)** β†’ `abs(-5) = 5`
21
  - **Minimum (`min(x, y, ...)`)** β†’ `min(3, 5, 2) = 2`
22
  - **Maximum (`max(x, y, ...)`)** β†’ `max(3, 5, 2) = 5`
23
- - **Sine (`sin(x)`)** β†’ `sin(45) β‰ˆ 0.707` (if degrees)
24
- - **Cosine (`cos(x)`)** β†’ `cos(45) β‰ˆ 0.707` (if degrees)
25
- - **Tangent (`tan(x)`)** β†’ `tan(45) β‰ˆ 1` (if degrees)
26
  - **Parentheses (`()`)** β†’ Ensures correct order of operations β†’ `(5 + 3) * 2 = 16`
27
 
28
- ⚠️ **If "Use Degrees" is enabled, trigonometric functions will automatically convert degrees to radians.**
 
 
29
  """)
30
 
31
  # User input for mathematical expression
@@ -33,20 +35,20 @@ def main():
33
 
34
  if expression:
35
  try:
36
- # Function wrapper to convert degrees to radians if needed
37
- def sin_deg(x):
38
- return math.sin(math.radians(x)) if use_degrees else math.sin(x)
39
 
40
- def cos_deg(x):
41
- return math.cos(math.radians(x)) if use_degrees else math.cos(x)
42
 
43
- def tan_deg(x):
44
- return math.tan(math.radians(x)) if use_degrees else math.tan(x)
45
 
46
  # Safe evaluation using eval with restricted built-in functions
47
  result = eval(expression, {"__builtins__": None},
48
  {"abs": abs, "min": min, "max": max,
49
- "sin": sin_deg, "cos": cos_deg, "tan": tan_deg})
50
 
51
  st.success(f"Result: {result}")
52
  except Exception as e:
 
4
  def main():
5
  st.title("Ultimate Scientific Calculator")
6
 
7
+ # User chooses between degrees and radians
8
+ angle_mode = st.radio("Select Angle Mode:", ["Degrees", "Radians"], index=0)
9
 
10
  # Display supported operations
11
  st.markdown("""
 
20
  - **Absolute Value (`abs(x)`)** β†’ `abs(-5) = 5`
21
  - **Minimum (`min(x, y, ...)`)** β†’ `min(3, 5, 2) = 2`
22
  - **Maximum (`max(x, y, ...)`)** β†’ `max(3, 5, 2) = 5`
23
+ - **Sine (`sin(x)`)** β†’ `sin(45) β‰ˆ 0.707` (if Degrees)
24
+ - **Cosine (`cos(x)`)** β†’ `cos(45) β‰ˆ 0.707` (if Degrees)
25
+ - **Tangent (`tan(x)`)** β†’ `tan(45) β‰ˆ 1` (if Degrees)
26
  - **Parentheses (`()`)** β†’ Ensures correct order of operations β†’ `(5 + 3) * 2 = 16`
27
 
28
+ ⚠️ **Angle Mode:**
29
+ - If **Degrees Mode is selected**, `sin(45)` is treated as `sin(45Β°)`.
30
+ - If **Radians Mode is selected**, `sin(3.14)` is treated as `sin(3.14 radians)`.
31
  """)
32
 
33
  # User input for mathematical expression
 
35
 
36
  if expression:
37
  try:
38
+ # Convert angles to radians if needed
39
+ def sin_angle(x):
40
+ return math.sin(math.radians(x)) if angle_mode == "Degrees" else math.sin(x)
41
 
42
+ def cos_angle(x):
43
+ return math.cos(math.radians(x)) if angle_mode == "Degrees" else math.cos(x)
44
 
45
+ def tan_angle(x):
46
+ return math.tan(math.radians(x)) if angle_mode == "Degrees" else math.tan(x)
47
 
48
  # Safe evaluation using eval with restricted built-in functions
49
  result = eval(expression, {"__builtins__": None},
50
  {"abs": abs, "min": min, "max": max,
51
+ "sin": sin_angle, "cos": cos_angle, "tan": tan_angle})
52
 
53
  st.success(f"Result: {result}")
54
  except Exception as e: