About the calculation engine
This stuff is for those who have special interest in the method the calculation engine uses to calculate the answer. For those who are not, its not important as it is taken care of.
The calculator uses a recursive model and a data stack to evaluate formulas. Operators at the same priority are evaluated from left to right. For example:
This is evaluated successively: 2+3=5, then 5+4=9, 9+5=14, and finally 14+6=20.
With operators of different priorities:
Before calculating 7+3, the engine sees that multiplication has higher priority. It calculates 3*4*5=60, then sees that the power operation has still higher priority and calculates 6^2=36. It then completes 60*36=2160, followed by 7+2160=2167 and finally 2167+8=2175.
Only 2 is the exponent of 6. To place 2+8 in the exponent, the expression would have to be written explicitly as 6^(2+8).
When we come to parenthesis, a parenthesis with a formula, basically takes position of a factor in the equation. When we look at a factor, it can be either a number, a constant, a variable, a function, or a parenthesis. Every one of these, is calculated into a number, before we go further in the calculation. The variables and constant is taken from the memory, the parenthesis, we do escalate to a higher recursion level to calculate the content of the parenthesis. The same with a function as it have its argument enclosed in parenthesis, it might be a new formula. When we encounter the end parenthesis, we return back one level. Now we have a single number to perform the function on, before we go on with the next operand on this level.
We see that we now have broken the whole thing down to factors and operands. Both have its different recursion point, while the operands decide its recursion on the next operands priority compared to the one it is to perform. The factors recursion perform a complete evaluation of contents of parenthesis to turn its result into a number, a factor to go on with. Functions do the same, but perform the function on the result returning the result as a number, a factor to go on with.
If there is no end of parenthesis, the end of formula will act as one. All the recursion levels will end at the end of the formula, performing its functions to complete the formula. We do complete even if not all parenthesis are closed. For intermediate result, most calculators present only the result on the parenthesis level we HAVE closed. We do not, we complete as they where closed. It is more likely that one has forgotten to close a parenthesis, than leaving parts of the formula un-calculated.
Many calculators do register-math or stack-math on the operations as we type the operands, with no memory of what has been done before. Then it is more natural to only present the top of the stack while we go. Here, we do not preserve stack from one operation to the next, - we do store the keystrokes in the formula, - to re-evaluate the whole formula each time something new is appended to it.
The advantage to this, is that the user can see what he has entered in the formula, and then be more confident that the result is correct. It is easy to do something wrong, and in this way, it can be spotted and corrected instead of re-calculate the formula to see if you got the same answer.
For example, the complete inner, outer, and end area of a tube can be expressed as:
Because FormulaCalculator retains the expression, its operators and variable values remain visible for checking and later editing.
