Spaces:
Sleeping
Sleeping
File size: 3,469 Bytes
db854fc c85f41c db854fc c85f41c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 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}")
|