MASS24's picture
Update app.py
c85f41c verified
import streamlit as st
import math
st.title("🧮 Advanced Calculator")
tab1, tab2, tab3 = st.tabs(["Basic Operations", "Scientific Functions", "Trigonometry"])
with tab1:
st.header("Basic Operations")
num1 = st.number_input("Enter first number", key="basic_num1")
num2 = st.number_input("Enter second number", key="basic_num2")
operation = st.selectbox("Choose operation", ["Add (+)", "Subtract (-)", "Multiply (*)", "Divide (/)"], key="basic_op")
if st.button("Calculate", key="basic_calc"):
if operation == "Add (+)":
result = num1 + num2
elif operation == "Subtract (-)":
result = num1 - num2
elif operation == "Multiply (*)":
result = num1 * num2
elif operation == "Divide (/)":
if num2 != 0:
result = num1 / num2
else:
st.error("Cannot divide by zero.")
result = None
if result is not None:
st.success(f"Result: {result}")
with tab2:
st.header("Scientific Functions")
num = st.number_input("Enter a number", key="sci_num")
function = st.selectbox("Choose function", ["Power (x^y)", "Square Root", "Log10", "Natural Log", "Factorial"], key="sci_func")
if function == "Power (x^y)":
exponent = st.number_input("Enter exponent", key="sci_exponent")
if st.button("Calculate", key="sci_calc"):
try:
if function == "Power (x^y)":
result = math.pow(num, exponent)
elif function == "Square Root":
if num < 0:
st.error("Square root of negative number is not supported.")
result = None
else:
result = math.sqrt(num)
elif function == "Log10":
if num <= 0:
st.error("Logarithm undefined for zero or negative numbers.")
result = None
else:
result = math.log10(num)
elif function == "Natural Log":
if num <= 0:
st.error("Natural log undefined for zero or negative numbers.")
result = None
else:
result = math.log(num)
elif function == "Factorial":
if num < 0 or int(num) != num:
st.error("Factorial only defined for non-negative integers.")
result = None
else:
result = math.factorial(int(num))
if result is not None:
st.success(f"Result: {result}")
except Exception as e:
st.error(f"Error: {e}")
with tab3:
st.header("Trigonometric Functions")
angle = st.number_input("Enter angle in degrees", key="trig_angle")
trig_func = st.selectbox("Choose function", ["sin", "cos", "tan"], key="trig_func")
if st.button("Calculate", key="trig_calc"):
rad = math.radians(angle)
if trig_func == "sin":
result = math.sin(rad)
elif trig_func == "cos":
result = math.cos(rad)
elif trig_func == "tan":
if math.isclose(math.cos(rad), 0, abs_tol=1e-10):
st.error("tan is undefined for this angle.")
result = None
else:
result = math.tan(rad)
if result is not None:
st.success(f"{trig_func}({angle}°) = {result}")