Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,90 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
st.title("Simple Calculator")
|
4 |
|
|
|
1 |
import streamlit as st
|
2 |
+
import math
|
3 |
+
|
4 |
+
st.title("Scientific Calculator")
|
5 |
+
|
6 |
+
# All operations
|
7 |
+
operations = [
|
8 |
+
"+", "-", "*", "/", "^ (power)", "% (modulus)", "// (floor division)",
|
9 |
+
"√ (square root)", "log10", "ln (natural log)", "n! (factorial)",
|
10 |
+
"sin", "cos", "tan", "exp (e^x)"
|
11 |
+
]
|
12 |
+
|
13 |
+
operation = st.selectbox("Select operation", operations)
|
14 |
+
|
15 |
+
# Input fields
|
16 |
+
num1 = st.number_input("Enter first number", format="%.4f")
|
17 |
+
|
18 |
+
# Only show num2 for operations that need two inputs
|
19 |
+
if operation in ["+", "-", "*", "/", "^ (power)", "% (modulus)", "// (floor division)"]:
|
20 |
+
num2 = st.number_input("Enter second number", format="%.4f")
|
21 |
+
else:
|
22 |
+
num2 = None
|
23 |
+
|
24 |
+
result = None
|
25 |
+
error = None
|
26 |
+
|
27 |
+
if st.button("Calculate"):
|
28 |
+
try:
|
29 |
+
if operation == "+":
|
30 |
+
result = num1 + num2
|
31 |
+
elif operation == "-":
|
32 |
+
result = num1 - num2
|
33 |
+
elif operation == "*":
|
34 |
+
result = num1 * num2
|
35 |
+
elif operation == "/":
|
36 |
+
if num2 != 0:
|
37 |
+
result = num1 / num2
|
38 |
+
else:
|
39 |
+
error = "Cannot divide by zero"
|
40 |
+
elif operation == "^ (power)":
|
41 |
+
result = num1 ** num2
|
42 |
+
elif operation == "% (modulus)":
|
43 |
+
if num2 != 0:
|
44 |
+
result = num1 % num2
|
45 |
+
else:
|
46 |
+
error = "Cannot perform modulus by zero"
|
47 |
+
elif operation == "// (floor division)":
|
48 |
+
if num2 != 0:
|
49 |
+
result = num1 // num2
|
50 |
+
else:
|
51 |
+
error = "Cannot perform floor division by zero"
|
52 |
+
elif operation == "√ (square root)":
|
53 |
+
if num1 >= 0:
|
54 |
+
result = math.sqrt(num1)
|
55 |
+
else:
|
56 |
+
error = "Cannot take square root of a negative number"
|
57 |
+
elif operation == "log10":
|
58 |
+
if num1 > 0:
|
59 |
+
result = math.log10(num1)
|
60 |
+
else:
|
61 |
+
error = "Log10 undefined for non-positive numbers"
|
62 |
+
elif operation == "ln (natural log)":
|
63 |
+
if num1 > 0:
|
64 |
+
result = math.log(num1)
|
65 |
+
else:
|
66 |
+
error = "Natural log undefined for non-positive numbers"
|
67 |
+
elif operation == "n! (factorial)":
|
68 |
+
if num1 >= 0 and num1 == int(num1):
|
69 |
+
result = math.factorial(int(num1))
|
70 |
+
else:
|
71 |
+
error = "Factorial only defined for non-negative integers"
|
72 |
+
elif operation == "sin":
|
73 |
+
result = math.sin(math.radians(num1))
|
74 |
+
elif operation == "cos":
|
75 |
+
result = math.cos(math.radians(num1))
|
76 |
+
elif operation == "tan":
|
77 |
+
result = math.tan(math.radians(num1))
|
78 |
+
elif operation == "exp (e^x)":
|
79 |
+
result = math.exp(num1)
|
80 |
+
except Exception as e:
|
81 |
+
error = str(e)
|
82 |
+
|
83 |
+
if error:
|
84 |
+
st.error(error)
|
85 |
+
else:
|
86 |
+
st.success(f"Result: {result:.6f}")
|
87 |
+
import streamlit as st
|
88 |
|
89 |
st.title("Simple Calculator")
|
90 |
|