nosheen80's picture
Update app.py
094ab23 verified
Raw History Blame Contribute Delete
2.79 kB
import streamlit as st
import numpy as np
import math
st.set_page_config(page_title="Scientific Calculator", layout="centered")
st.title("🔬 Advanced Scientific Calculator")
st.markdown("Use the fields below to calculate using scientific functions or direct expressions.")
calc_mode = st.radio("Choose Mode", ["Button-Based Calculator", "Expression Evaluator"])
if calc_mode == "Expression Evaluator":
expr = st.text_input("Enter expression (e.g. 2 * np.sin(np.pi/4) + np.log10(100))")
if st.button("Evaluate Expression"):
try:
result = eval(expr, {"__builtins__": None}, {
"np": np, "math": math,
"sin": np.sin, "cos": np.cos, "tan": np.tan,
"log": np.log, "log10": np.log10,
"sqrt": np.sqrt, "pi": np.pi, "e": np.e,
"pow": np.power, "factorial": math.factorial
})
st.success(f"Result: {result}")
except Exception as e:
st.error(f"Error: {str(e)}")
else:
num1 = st.number_input("Enter First Number:", format="%.6f", step=0.1)
operation = st.selectbox("Choose Operation", [
"Add", "Subtract", "Multiply", "Divide", "Power", "Square Root",
"Sine", "Cosine", "Tangent", "Log10", "Natural Log (ln)", "Factorial"
])
if operation not in ["Sine", "Cosine", "Tangent", "Square Root", "Log10", "Natural Log (ln)", "Factorial"]:
num2 = st.number_input("Enter Second Number:", format="%.6f", step=0.1)
if st.button("Calculate"):
try:
if operation == "Add":
result = num1 + num2
elif operation == "Subtract":
result = num1 - num2
elif operation == "Multiply":
result = num1 * num2
elif operation == "Divide":
result = num1 / num2 if num2 != 0 else "Error: Division by zero"
elif operation == "Power":
result = math.pow(num1, num2)
elif operation == "Square Root":
result = math.sqrt(num1)
elif operation == "Sine":
result = np.sin(np.radians(num1))
elif operation == "Cosine":
result = np.cos(np.radians(num1))
elif operation == "Tangent":
result = np.tan(np.radians(num1))
elif operation == "Log10":
result = math.log10(num1)
elif operation == "Natural Log (ln)":
result = math.log(num1)
elif operation == "Factorial":
result = math.factorial(int(num1))
else:
result = "Invalid Operation"
st.success(f"Result: {result}")
except Exception as e:
st.error(f"Error: {str(e)}")