Spaces:
Sleeping
Sleeping
import streamlit as st | |
import math | |
st.title("Scientific Calculator") | |
# All operations | |
operations = [ | |
"+", "-", "*", "/", "^ (power)", "% (modulus)", "// (floor division)", | |
"β (square root)", "log10", "ln (natural log)", "n! (factorial)", | |
"sin", "cos", "tan", "exp (e^x)" | |
] | |
operation = st.selectbox("Select operation", operations) | |
# Input fields | |
num1 = st.number_input("Enter first number", format="%.4f") | |
# Only show num2 for operations that need two inputs | |
if operation in ["+", "-", "*", "/", "^ (power)", "% (modulus)", "// (floor division)"]: | |
num2 = st.number_input("Enter second number", format="%.4f") | |
else: | |
num2 = None | |
result = None | |
error = None | |
if st.button("Calculate"): | |
try: | |
if operation == "+": | |
result = num1 + num2 | |
elif operation == "-": | |
result = num1 - num2 | |
elif operation == "*": | |
result = num1 * num2 | |
elif operation == "/": | |
if num2 != 0: | |
result = num1 / num2 | |
else: | |
error = "Cannot divide by zero" | |
elif operation == "^ (power)": | |
result = num1 ** num2 | |
elif operation == "% (modulus)": | |
if num2 != 0: | |
result = num1 % num2 | |
else: | |
error = "Cannot perform modulus by zero" | |
elif operation == "// (floor division)": | |
if num2 != 0: | |
result = num1 // num2 | |
else: | |
error = "Cannot perform floor division by zero" | |
elif operation == "β (square root)": | |
if num1 >= 0: | |
result = math.sqrt(num1) | |
else: | |
error = "Cannot take square root of a negative number" | |
elif operation == "log10": | |
if num1 > 0: | |
result = math.log10(num1) | |
else: | |
error = "Log10 undefined for non-positive numbers" | |
elif operation == "ln (natural log)": | |
if num1 > 0: | |
result = math.log(num1) | |
else: | |
error = "Natural log undefined for non-positive numbers" | |
elif operation == "n! (factorial)": | |
if num1 >= 0 and num1 == int(num1): | |
result = math.factorial(int(num1)) | |
else: | |
error = "Factorial only defined for non-negative integers" | |
elif operation == "sin": | |
result = math.sin(math.radians(num1)) | |
elif operation == "cos": | |
result = math.cos(math.radians(num1)) | |
elif operation == "tan": | |
result = math.tan(math.radians(num1)) | |
elif operation == "exp (e^x)": | |
result = math.exp(num1) | |
except Exception as e: | |
error = str(e) | |
if error: | |
st.error(error) | |
else: | |
st.success(f"Result: {result:.6f}") | |