How CrunchJr Works



CrunchJr is a powerful computational engine.  It allows you to enter an expression in the Submit box, and evaluate it for multiple values of the variables.  For example,

beta*sin(x-sqrt(beta))/(PI*x)

has two variables (beta and x).  Entering 4.002 and 3.73 for beta and 4.11, 4.7, 4.447 for x produces the output on the right (note that the highlighting of rows 3 and 4 is only to make it easier to read).  This page describes how the computation is performed.



CrunchJr reads expressions in the standard infix form, but stores them in postfix ("Reverse Polish") form.  Reverse Polish is named for the Polish mathematician, Jan Lukasiewicz.

Infix  |  Postfix
X + Y  |  X Y +
X * Y + Z  |  X Y * Z +
X + Y * Z  |  X Y Z * +
(X - Y) / Sin(R - T)  |  X Y - R T - Sin /
Sqrt(A+R)*Max(R,R-A,A+1)      |  A R + Sqrt R R A - A 1 + Max *

Postfix representation has several advantages over infix for computer evaluation:  Postfix is expressed without parentheses, postfix is easy to evaluate using stacks, and postfix does not have precedence rules.  The translation of infix to postfix notation handles the parentheses and precedence rules once, so that multiple evaluations of the expression with different values for the variables can be more efficient.  Note that by convention, Expression 3 above, X+Y*Z is evaluated as X+(Y*Z) and not (X+Y)*Z.  That is, multiplication has a higher precedence than addition.

The valid operators are   +   −   *   /   %   **   ^   (and function calls).   The precedence rules are

Operator    Precedence Description
(1 Open Parenthesis
)1 Close Parenthesis
Func()2Functions
**3Exponentiation
^3Exponentiation
*4Multiplication
/4Division
%4Remainder
+5Addition
5Subtraction

Operators of equal precedence are resolved left-to-right

During evaluation, CrunchJr "pops" items off the stack from left to right.  Each item is either a constant, a variable, or an operator.   Each variable is replaced with one of its values.   Each time an operator is encountered, it operates on the appropriate number of "popped" values, and "pushes" the result back onto the stack.  These steps are repeated until only the final result remains on the stack.   Then the whole procedure is repeated for the next set of variable values.

~~~//~~~