A Parser, Not an Evaluator of Code
Most quick calculators on the web hand the expression to the JavaScript engine behind a character filter. That is convenient and unsafe: the filter has to anticipate every way a name can be reached, and the code still runs with the page\u2019s full privileges. This tool does not evaluate code at all. It tokenises the expression, parses it with a recursive descent parser against a grammar that contains only numbers, the operators plus, minus, times, divide, modulo and power, parentheses, three constants and a fixed list of math functions, and computes the result from that tree. Anything outside the grammar is a parse error with the position marked, so an unknown name is reported rather than resolved. Precedence follows normal mathematical convention: power binds tightest and is right associative, so 2^3^2 is 512, and a leading minus applies after the power, so -2^2 is -4.
How to Use the Math Evaluator
- 1Type an expression such as (2 + 3) * 4 or round(pi * 100) / 100.
- 2Use ^ or ** for powers, and % for the remainder.
- 3Call any of the supported functions, such as sqrt, pow, min, max, hypot, log, ln and the trigonometric ones. Names are case-insensitive.
- 4Use pi, e and tau where you need a constant.
- 5If the expression is malformed, read the error, which names the character or token and its position.
Common Math Evaluator Use Cases
Work out a value while writing code
Compute a buffer size, a timeout in milliseconds or a percentage without leaving the page or opening a REPL.
Check operator precedence
Confirm how an expression groups before relying on it, particularly around powers and unary minus.
Convert with a formula
Apply a one-off formula, such as a unit conversion or an interest calculation, with functions rather than by hand.
Evaluate an expression from an untrusted place
Paste an expression from a ticket or a log knowing it is parsed as arithmetic and cannot run as code.
Frequently asked questions
Why is -2^2 negative?
Because exponentiation binds more tightly than a leading sign, so the expression means -(2^2). This is the usual mathematical reading and matches what a scientific calculator does. Write (-2)^2 when you want the sign included in the base, which gives 4.
Why does an unknown name produce an error rather than a result?
Because the parser has no access to anything beyond its own table of functions and constants. There is no scope to fall back to, so a name it does not know cannot mean anything. That is deliberate: it is what stops an expression from reaching the page.
Can this run JavaScript?
No. It never calls eval or the Function constructor. The expression is turned into tokens and then into a small syntax tree containing only numbers and the permitted operations, so there is nothing for an injected expression to reach.
Why do decimals sometimes look slightly off?
Arithmetic uses IEEE 754 double precision, the same as every other tool in a browser, so 0.1 + 0.2 is 0.30000000000000004. That is a property of binary floating point, not a parsing error. Round the result when you need a fixed number of decimal places.