A scientific calculator that does the maths the safe way
This calculator handles everything a classroom or workbench scientific calculator does — the
four basic operators, parentheses, percentages, powers and roots, the three core trigonometric
functions, natural and base-10 logarithms, factorials, the constants π and
e, and a four-key memory. What makes it different is what happens under the hood:
it never calls JavaScript’s eval(). Instead it ships a small, purpose-built
math engine, so your expression is treated as data to be measured, not code to be run.
How the math engine works
Every time you press =, your expression travels through three stages:
- Tokenize. The raw string is scanned left to right and broken into
tokens: numbers, operators (
+ - * / ^ %), function names (sin,ln, …) and parentheses. - Shunting-yard. Edsger Dijkstra’s algorithm reorders those tokens from
ordinary infix notation into Reverse Polish Notation (RPN), using
an operator stack so that precedence (
*before+) and parentheses are honoured. The rule is simple:while the operator on top of the stack has greater or equal precedence, pop it to the output, then push the new operator. - Evaluate. The RPN is run through a stack machine. Numbers are pushed; an operator pops the right number of operands, computes, and pushes the result. The single value left on the stack is the answer.
A worked example: 2 + 3 × 4
Tokenizing gives 2, +, 3, *, 4.
Because * binds tighter than +, the shunting-yard step produces the
RPN 2 3 4 * +. The stack machine then pushes 2, 3 and 4; the * pops
3 and 4 to compute 3 × 4 = 12, leaving 2 and 12; finally +
computes 2 + 12 = 14. Wrapping the start in parentheses,
(2 + 3) × 4, reorders the RPN to 2 3 + 4 * and the result
becomes 20 — exactly the difference parentheses are supposed to make.
Functions and what they return
| Key | Meaning | Example |
|---|---|---|
sin / cos / tan | Trigonometry (DEG or RAD) | sin(30) = 0.5 in DEG |
ln | Natural log (base e) | ln(e) = 1 |
log | Common log (base 10) | log(1000) = 3 |
√ | Square root | sqrt(16) = 4 |
x^y | Power | 2^10 = 1024 |
x! | Factorial | 5! = 120 |
1/x | Reciprocal | 1/x of 4 = 0.25 |
eval() is never used, your input can
never be executed as code. Close the tab and nothing remains.