Laiba-Huggingface's picture
Update app.py
8c28bb8 verified
import streamlit as st
import math
def main():
st.title("Ultimate Scientific Calculator")
# User chooses between degrees and radians
angle_mode = st.radio("Select Angle Mode:", ["Degrees", "Radians"], index=0)
# Display supported operations
st.markdown("""
## πŸ”Ή Supported Operations:
- **Addition (`+`)** β†’ `5 + 3 = 8`
- **Subtraction (`-`)** β†’ `10 - 4 = 6`
- **Multiplication (`*`)** β†’ `7 * 2 = 14`
- **Division (`/`)** β†’ `8 / 2 = 4.0`
- **Exponentiation (`**`)** β†’ `3 ** 2 = 9`
- **Modulus (`%`)** β†’ `10 % 3 = 1`
- **Floor Division (`//`)** β†’ `10 // 3 = 3`
- **Absolute Value (`abs(x)`)** β†’ `abs(-5) = 5`
- **Minimum (`min(x, y, ...)`)** β†’ `min(3, 5, 2) = 2`
- **Maximum (`max(x, y, ...)`)** β†’ `max(3, 5, 2) = 5`
- **Sine (`sin(x)`)** β†’ `sin(45) β‰ˆ 0.707` (if Degrees)
- **Cosine (`cos(x)`)** β†’ `cos(45) β‰ˆ 0.707` (if Degrees)
- **Tangent (`tan(x)`)** β†’ `tan(45) β‰ˆ 1` (if Degrees)
- **Parentheses (`()`)** β†’ Ensures correct order of operations β†’ `(5 + 3) * 2 = 16`
⚠️ **Angle Mode:**
- If **Degrees Mode is selected**, `sin(45)` is treated as `sin(45Β°)`.
- If **Radians Mode is selected**, `sin(3.14)` is treated as `sin(3.14 radians)`.
""")
# User input for mathematical expression
expression = st.text_input("Enter your mathematical expression:", "")
if expression:
try:
# Convert angles to radians if needed
def sin_angle(x):
return math.sin(math.radians(x)) if angle_mode == "Degrees" else math.sin(x)
def cos_angle(x):
return math.cos(math.radians(x)) if angle_mode == "Degrees" else math.cos(x)
def tan_angle(x):
return math.tan(math.radians(x)) if angle_mode == "Degrees" else math.tan(x)
# Safe evaluation using eval with restricted built-in functions
result = eval(expression, {"__builtins__": None},
{"abs": abs, "min": min, "max": max,
"sin": sin_angle, "cos": cos_angle, "tan": tan_angle})
st.success(f"Result: {result}")
except Exception as e:
st.error(f"Error in calculation: {e}")
if __name__ == "__main__":
main()