How a typed expression is parsed and evaluated in real numbers
This expression calculator evaluates real-number arithmetic through a limited, documented grammar. It separates tokenization, parsing, an abstract syntax tree and evaluation so the expression is treated as math input rather than executable JavaScript. Trigonometric results depend on the angle mode selected by the visitor.
The supported grammar is a site-defined real-number language, not JavaScript. It includes decimal numbers (also 1e-3 notation), binary +, −, ×, ÷ and ^, unary +/−, parentheses, functions sin/cos/tan/asin/acos/atan/sqrt/ln/log10/abs, and constants pi/e. Keyboard * and / represent × and ÷. Functions require one parenthesized argument. Exponentiation is right-associative and binds before unary minus; adjacent values are never multiplied implicitly.
The parser exposes a small, documented grammar with an explicit angle mode. Errors identify invalid syntax or domains instead of treating the entered text as executable code or silently changing its meaning.
How the expression syntax and the angle mode change the result
There are two inputs, but the expression has its own small grammar. These notes cover the rules most likely to produce a different number from the one you expected.
- Expression
- Up to 256 characters using numbers, + − × ÷ ^ (or * and /), parentheses, ten named functions, and the constants pi and e. Names are case-insensitive, so SIN(30) works. A percent sign, factorial or the bare name log is not in the grammar and stops the calculation with an error naming the problem.
- Scientific notation or the constant e
- A number followed directly by e and an exponent is read as notation, so 2e3 is 2000. Standing alone, e is Euler's number, so 2*e is 5.43656365692. Typing 2e with no exponent is treated as implicit multiplication and rejected rather than guessed.
- Angle mode
- Degrees or Radians applies to every sin, cos and tan argument and to every asin, acos and atan result in the expression. sin(30) is 0.5 in degrees but −0.988031624093 in radians, and asin(0.5) returns 30 or 0.523598775598. Expressions without trigonometry give the same answer in either mode.
- Logarithms
- ln is the natural logarithm and log10 the base-10 logarithm; there is no log with an optional base, so log10(100) returns 2. Both reject zero and negative arguments. For another base b, divide two logs, for example ln(8)/ln(2) for log base 2 of 8.
- Twelve-digit display
- The result is rounded to 12 significant digits, so 1/3 displays 0.333333333333 and 0.1+0.2 displays 0.3. Values that should be exactly zero can keep a tiny floating-point remainder instead: sin(180) in degrees returns 1.22464679915e-16, which is zero for practical purposes.
Steps
- Type a real-number expression with explicit operators. Supported functions are sin, cos, tan, asin, acos, atan, sqrt, ln, log10 and abs; constants are pi and e.
- Use parentheses around each function's single argument, for example sqrt(9). Write 2*pi, not 2pi.
- Choose Degrees or Radians before calculating trig or inverse trig expressions.
- Read the approximate numerical result; domain errors and unsupported syntax are reported instead of guessed.
Reading tip: Compare -2^2 with (-2)^2, and check Degrees/Radians before using trig. A domain or syntax error is reported without silently rewriting the expression.
Calculations that benefit from an explicit expression grammar
Evaluate an expression
Combine arithmetic, parentheses and right-associative powers in one documented grammar.
Check angle units
Compare trig calculations using an explicit Degrees or Radians setting.
Inspect a domain error
See why a negative square root or nonpositive logarithm has no supported real result.
Learn precedence
Compare -2^2, (-2)^2 and 2^3^2 using their displayed interpretation.
How the calculation works
- AST
- Abstract syntax tree built from supported math tokens; user text is not JavaScript.
- ^
- Right-associative exponentiation, evaluated before a leading unary minus.
- angle mode
- Explicit Degrees or Radians choice for trig inputs and inverse-trig outputs.
- domain
- Allowed real inputs for each operation, such as positive logarithm arguments.
This is a product-defined grammar. It never executes user text as JavaScript; floating-point output is approximate.
What every part means
- AST
- Abstract syntax tree built from supported math tokens; user text is not JavaScript.
- ^
- Right-associative exponentiation, evaluated before a leading unary minus.
- angle mode
- Explicit Degrees or Radians choice for trig inputs and inverse-trig outputs.
- domain
- Allowed real inputs for each operation, such as positive logarithm arguments.
Calculate it step by step
An unknown character or identifier is rejected; the text is never run as JavaScript.
Thus -2^2=−4, 2^3^2=512, and 2^-2=0.25.
Division, domains, discontinuities and non-finite results are checked before display.
The tokenizer, parser, AST and evaluator are product-created technical rules; no paper is credited with this grammar. Input length is 1–256 characters and parser/evaluator nesting depth is at most 32. Tangent is rejected when |cos(angle)| < 10^-12 to avoid returning an enormous rounding artifact near an undefined point; that threshold is a numerical guard, not a mathematical identity.
See how Calculation Guide tests examples and handles calculation limits.
See it in practice
- Expression
- -2^2 + sin(30)
- Angle mode
- Degrees
Degrees mode: -2^2 + sin(30) = -(2^2) + 0.5 = -3.5. Also 2^3^2 = 2^9 = 512 and 2^-2 = 0.25.
- Angle modeDegrees
- Expression-2^2 + sin(30)
Interpretation & limitations
How to read your result
Results are approximate because JavaScript's finite floating-point representation is used for numerical evaluation. Degrees mode converts trig arguments to radians internally and inverse-trig results back to degrees. Functions and constants use their documented mathematical meanings.
Where this estimate stops
This tool does not handle complex numbers, symbolic simplification, factorial, percent, assignments or implicit multiplication. 0^0 is rejected as a documented product choice. Longer expressions, deep trees and non-finite outputs are rejected rather than executed or truncated.
Frequently asked questions
01Why is -2^2 equal to -4 here?
Exponentiation binds before unary minus, so the expression means −(2²). Use (-2)^2 for +4.
02Why does 2pi return an error?
Implicit multiplication is outside this parser's grammar. Enter 2*pi explicitly.
03What does 2^3^2 mean?
Exponentiation groups from the right in this grammar: 2^(3^2) = 512.
04Can I type sin 30?
No. Write sin(30), with Degrees selected if you mean 30 degrees.
05Why can tan(90) return an error?
In Degrees mode, 90° is a tangent discontinuity; near it numerical output is unreliable at the tool's precision.
06Why are some decimals approximate?
Finite floating-point arithmetic cannot represent every real number exactly. The display is capped at 12 significant digits.
07How do I take the cube root of a negative number?
The tool rejects (-8)^(1/3), because a negative base with a fractional exponent has no value in the real-number power function it uses. Leave the minus outside the power instead: -8^(1/3) returns −2, since exponentiation is evaluated before the leading minus. This works for odd roots only; an even root of a negative number has no real answer.