The parser's precedence ladder
NumUtility evaluates grouping and function arguments, completes a primary value and its optional factorial, applies exponentiation, processes unary plus or minus, performs multiplication and division from left to right, and finally performs addition and subtraction from left to right. This operational description is more precise than memorizing PEMDAS because the mnemonic does not specify every calculator's treatment of unary signs, chained powers, functions, or postfix operators.
Every expression should be read according to the parser actually used. Parentheses can override or clarify the default. The calculator removes spaces and lowercases function names, but it does not insert missing operators. A syntactically familiar expression from a textbook may still need an asterisk where print typography implies multiplication. This page documents current behavior; it is not a universal claim about every programming language or calculator.
Grouping and function calls
Parentheses force the enclosed expression to be evaluated as one group and may be nested. In (3+5)*2^2, the group becomes 8, the power becomes 4, and the product is 32. Function parentheses also define an argument: sqrt(9+7) computes sqrt(16), not sqrt(9)+7. A missing closing parenthesis produces an error rather than an inferred group.
Function calls have names directly followed by parentheses. sin 30 and log 1000 are invalid even if a physical calculator accepts key-by-key entry. The supported functions are sqrt, sin, cos, tan, ln, log, and abs. An expression such as abs(-3^2) first evaluates -3² as -9 inside the function and then returns 9. Writing abs((-3)^2) returns 9 through a different intermediate value.
Factorial and exponentiation
A factorial attaches to the completed primary immediately before it. Thus 3!^2 means (3!)²=36, while 2^3! means 2^(3!)=64. A parenthesized group may be factorialized: (3+2)!=120. The parser accepts only one exclamation mark and restricts the operand to an integer from 0 through 170. Double factorial notation such as 7!! is not implemented.
Exponentiation is right-associative because the exponent is parsed through the unary-and-power rule again. Therefore 2^3^2 means 2^(3²)=512, not (2³)²=64. Parentheses select the alternative explicitly. Powers are evaluated before multiplication and addition, but a signed exponent is allowed: 2^-2=1/4. A nonfinite power result is rejected by the final validation.
Unary minus and negative bases
Unary minus applies outside an unparenthesized power. The expression -2^2 is therefore -(2²)=-4. To square the negative base, write (-2)^2=4. This matches a common mathematical convention but differs from some spreadsheet or programming syntaxes, so parentheses are the safest portable notation. Unary plus is accepted and does not change the value.
A negative exponent works because the sign belongs to the exponent: 2^-3=1/8. A negative base with a noninteger exponent may produce NaN in real floating-point arithmetic and is rejected. Factorial of -3 is invalid whether written (-3)! or as another arrangement, because the factorial domain is nonnegative integers. These cases show why the visual location and scope of a sign matter.
Three parser-verified examples
Input: 3 + 2*5^2. Rule: power, then multiplication, then addition. Steps: 5²=25; 2*25=50; 3+50=53. Result: 53. Verification: the project test evaluates this exact expression and asserts 53.
Input: 2^3^2. Rule: chained powers associate to the right. Steps: evaluate 3²=9, then 2^9. Result: 512. Verification: the alternative left grouping (2³)² equals 64 and requires explicit parentheses.
Input: -2^2 and (-2)^2. Rule: exponent precedes an external unary minus, while parentheses make the sign part of the base. Steps: -(2²)=-4; the grouped base gives (-2)×(-2)=4. Result: -4 and 4. Verification: entering both expressions produces different signs exactly as the grouping predicts.
Equal-precedence operations run left to right
Multiplication and division share one precedence level and are applied in encounter order. The expression 24/6*2 becomes 4*2=8, not 24/12=2. Addition and subtraction likewise run left to right: 10-3+2 becomes 7+2=9. The letters in PEMDAS should not be read as “all multiplication before all division” or “all addition before all subtraction.”
Parentheses can document the intended alternative: 24/(6*2)=2 and 10-(3+2)=5. When translating a stacked fraction, place the complete numerator and denominator in parentheses. Write (a+b)/(c+d), not a+b/c+d. Explicit grouping is especially important when a formula contains nested quotients, negative values, or a power applied to an entire fraction.
Syntax that precedence cannot repair
Implicit multiplication is unsupported. The parser rejects 2pi and 2(3+4), so no precedence rule is ever applied; use 2*pi and 2*(3+4). It also rejects adjacent function calls without an operator. Scientific notation such as 1e3 is supported as part of a numeric literal, but the constant e by itself is Euler's number. The surrounding characters determine which meaning is parsed.
A valid parse can still be outside a mathematical domain. sqrt(-1), log(0), and division by zero do not become valid through rearranged precedence. Conversely, a generic invalid-expression message may represent syntax, a NaN-producing real-domain operation, or an infinite result. Test the smallest suspect subexpression to separate grammar from mathematics.
Writing expressions for review
Use parentheses to make every disputed scope visible, even when defaults would give the same result. Write 3+(2*(5^2)) when teaching the evaluation tree, (-2)^4 for a negative base, and 2^(3^2) for a power tower. This reduces reliance on a reader's remembered convention and makes the input easier to compare with a formula.
The NumUtility calculator is an evaluator, not a symbolic simplifier. It does not display an expression tree or intermediate steps, so this guide supplies the reasoning that the result screen omits. Verify examples with the current tool and preserve the typed string when reporting a result. If another system returns a different answer, compare grammar and associativity before assuming arithmetic failure.
Nested examples and translation traps
Input: sqrt((3+1)^2)+6/3. Rule: resolve the innermost group, power, function, and division before the final addition. Steps: 3+1=4; 4²=16; sqrt(16)=4; 6/3=2; then 4+2. Result: 6. Verification: entering the explicit expression returns 6, and each subexpression remains in the real domain.
A horizontal fraction bar groups its entire numerator and denominator even when typed notation does not show that automatically. The printed quantity (1+2)/(3+4) must retain both pairs of parentheses and equals 3/7. Typing 1+2/3+4 instead invokes division before addition and produces a different value. Radical bars and function notation likewise have visual scope that must be translated into parentheses.
Repeated postfix or implied operators are grammar questions, not precedence questions. The current parser accepts one factorial after a primary and requires every multiplication sign. It also requires the entire source to be consumed, so an otherwise valid prefix followed by unknown text fails rather than being silently ignored. This strictness prevents a partial answer from masquerading as evaluation of the full expression.
Precedence determines evaluation order, while associativity determines grouping among repeated operators of equal or special status. Keeping those terms separate makes disagreements easier to diagnose and review.