An expression calculator, not a keypad

Most pocket calculators make you press equals repeatedly and lose the whole expression the moment you mistype a digit. This page takes the other approach: you type the entire expression as one line, exactly the way it would be written on paper, and it is evaluated in a single pass. That means you can see the whole calculation, change one term, and re-run it without starting over.

The evaluator is a hand-written recursive-descent parser rather than a call to JavaScript's own eval, which is what makes the accepted syntax predictable and the error messages specific.

What the parser accepts

The operator set is deliberately small:

SymbolMeaning
+ −addition, subtraction
* or × or x or Xmultiplication
/ or ÷division
^exponent, applied right to left
%postfix per cent, dividing by 100
( )grouping
oftreated as multiplication

Precedence runs the conventional way: parentheses first, then exponents, then multiplication and division, then addition and subtraction. Unary minus is handled as a sign attached to the value that follows.

Worked expressions

Grouping and precedence. (12+8)*3^2 - 50/4 evaluates as 20 × 9 − 12.5 = 167.5. The parentheses are required: without them the expression would be a different sum entirely.

Per cent as a suffix. 20% of 250 becomes 0.2 × 250 = 50, because % divides the number in front of it by 100 and of is read as a multiplication. Writing 250*20% gives the same answer.

Right-associative powers. 2^3^2 is 2 raised to the 9th, or 512, not 8 squared. Stacked exponents group from the right, matching mathematical convention.

Sign before power. -2^2 returns −4, because the sign is applied to the completed power. Write (-2)^2 for positive four.

Binary noise is hidden by the output. 0.1+0.2 is not exactly 0.3 in binary floating point, but the result is displayed with at most ten decimal places, so the artefact never reaches the screen. It is still inside the value, which is why chaining many such additions can drift.

What this calculator will not do

Practical questions

How do I work out a per cent increase?

Multiply by one plus the rate. A 15 per cent rise on 80 is 80*1.15. For a decrease, multiply by 0.85. Chaining several adjustments in one line works: 199*0.8*0.9.

Why did my carefully typed expression come back invalid?

Nearly always one of three things: an unmatched parenthesis, a number with a comma in it, or a per cent sign used as if it were a remainder operator. The parser reports an invalid expression rather than guessing at a repair.

Can I reuse the result?

The copy button puts the displayed value on your clipboard so it can be pasted into the next step. There is no chain of stored intermediate answers.

How many decimal places will I see?

Up to ten, with trailing zeros removed. Whole results appear as whole numbers, so 6*7 shows 42 rather than 42.0000000000.

Is the per cent suffix the same as the Percentage page?

Related but narrower. The Percentage page asks and answers the three named percentage questions with labelled results; this page treats % as a plain divide-by-one-hundred instruction inside a longer expression. Use whichever matches the shape of the problem.