Expression Calculator
LifeSafely evaluate an arithmetic expression with + − × ÷ % ^ and parentheses. No eval, no code execution.
On this page
What is this calculator?#
It is a plain arithmetic calculator that evaluates a mathematical expression you type as text — (12 * 1.08) + 7 or 2^10 % 256 — and gives you the number. There are no function buttons to peck; you write the whole expression and press one key.
The important part is how it is built. Most quick calculators on the web cheat by handing your string to JavaScript’s eval(), which runs any code — fine for a toy, ugly the moment someone pastes something clever. This one does not: it runs a hand-written recursive-descent parser that accepts only numbers, parentheses, and the operators + - * / % ^. Anything outside that grammar is rejected on the spot. The result is a calculator that genuinely cannot run code, which is exactly why it is safe to paste into.
How to use it#
- Type your expression in the Input box. Whitespace is ignored, so
1+2and1 + 2are the same. - Operators:
+-*/for the four basics,%for modulo (the remainder),^for power. Parentheses()group. Unary minus works, so-5 + 3is-2. - Decimals and scientific notation are fine:
0.5,1e3(which is 1000),2.5e-2. - Click Evaluate (or press Enter). The Result line shows the number, or an inline message naming the problem —
Division by zero,Unexpected character,Mismatched parentheses, and so on — so you can fix the expression instead of guessing what went wrong. - Binary floating-point dust is cleaned off automatically, so
0.1 + 0.2shows0.3, not0.30000000000000004.
Key features#
- No
eval, nonew Function, no identifiers. Only numbers and seven operator characters are accepted — a fully bounded grammar, not arbitrary code execution. - Correct operator precedence (PEMDAS): parentheses, then
^, then* / %, then+ -. - Right-associative power.
2^3^2evaluates the way mathematicians write it (see the worked example). - Scientific notation (
6.022e23) and modulo (17 % 5) for real back-of-envelope work. - Friendly, specific errors that name the failure (divide-by-zero, truncated expression, bad number) with the offending position, instead of a silent
NaN.
Worked example#
Three expressions that each show a different behaviour. Paste them one at a time.
Operator precedence and grouping.
(1 + 2) * 3 ^ 2
Result: 27. The parentheses force 1 + 2 = 3 first; then 3 ^ 2 is evaluated before the multiply (power binds tighter than *), giving 9; finally 3 * 9 = 27. Without the parentheses, 1 + 2 * 3 ^ 2 would be 1 + 18 = 19.
Right-associative power.
2 ^ 3 ^ 2
Result: 512, not 64. Power associates to the right, so this is 2 ^ (3 ^ 2) = 2 ^ 9 = 512. If you actually wanted (2 ^ 3) ^ 2 = 64, write the parentheses.
Binary dust removed.
0.1 + 0.2
Result: 0.3. The raw binary floating-point sum is 0.30000000000000004; the formatter rounds it back to a sensible number of significant digits so the answer reads the way a human expects.
FAQ#
Why can’t I use sin, log, sqrt, or pi?#
Because those are identifiers, and identifiers are deliberately not part of the grammar. Every name is rejected as an Unexpected character — that is the line that keeps the calculator from ever executing code. If you need sqrt(16), rewrite it as 16 ^ 0.5; for log and sin, use a scientific calculator.
Does 2^3^2 give 512 or 64?#
512. The ^ operator is right-associative, matching the convention used in mathematics and in most programming languages’ power operator. 2^3^2 parses as 2^(3^2). To get 64, write (2^3)^2.
What is the % operator doing here?#
It is modulo — the remainder after integer division — not “percent”. 17 % 5 is 2, because 17 divided by 5 leaves a remainder of 2. It is handy for “is this number even?” (n % 2), wrapping values into a range, or clock arithmetic. It has the same precedence as * and /.
Is my expression sent to a server?#
No. Parsing and evaluation happen entirely in your browser with a hand-written parser — there is no backend, and the input never leaves the page.