On this page:
Hypatia's RPN, Hypatia's Enhanced RPN.
Hypatia uses an enhanced version of RPN. RPN is a form to write mathematical expressions that is an alternative to the much better known infix notation. Admittedly it’s a matter of taste, but while RPN can seem a bit confusing at first sight it actually follows a more concise logic, and does have advantages for simple as well as for complex calculations.
RPN does not use nor need parentheses, it only knows arguments (that is, numbers) and operators. It is strictly written from left to right, with all arguments and operators being separated by spaces.
Operators are written after their arguments. An operator performs a calculation upon one or more argument(s) which it finds to its left, and returns a single value. The operator "knows" how many arguments it has to process. After the operator has performed its calculation, it replaces its arguments and itself with the result.
Example: The + operator takes two arguments, and returns their sum. So, to perform an addition, you write the two arguments, and the + sign after them (always write spaces between numbers and operators):
5 4 +
The result, 9, will replace those three elements. This can be the end of the calculation, or it can become an argument for further operations:
5 4 + SQRT
SQRT, square root, is an operator that takes one argument. 5 and 4 are added up, making 9, then the operation "square root" is performed upon that result, so the final result is 3.
Conventional infix notation relies upon operator hierarchies and upon parentheses -- RPN knows neither, but relies exclusively upon the order in which arguments and operators are written:
5 4 SQRT +
In this example, the first operator that is encountered, reading from the left, is SQRT. It takes one argument, 4, and replaces it (and itself) with the square root, 2. Then, reading on, the operator + is encountered. It looks for two arguments to its left, and finds 5, still untouched by any previous operation, and 2, which now stands where "4 SQRT" had stood. The final result, therefore, is 7.
RPN knows no operator hierarchy. In infix notation, 2 + 3 * 5 + 7 means that the multiplication is performed before the additions, the result therefore is 24. We are so used to it that it seems as if this was some natural order of things, but in fact it is only an arbitrary rule. RPN treats all operators as being equal -- for the above calculation, you have to write:
2 3 5 * + 7 +
The first operator * multiplies 3 and 5, leaving 15, the following + operator has 2 and 15 to its left and adds them up to 17, and then the + operator adds up 17 and 7.
And, RPN does not use parentheses. Where in infix notation you would write (2 + 3) * (5 + 7), in RPN you would write:
2 3 + 5 7 + *
Here, the first + operator adds 2 and 3, returning the result 5. Then the next + operator adds 5 and 7, returning 12. The * operator, which is encountered next, finds those two results to its left, and therefore multiplies 5 and 12, returning the result 60.
For some operators that take two arguments the order of those arguments is relevant: it makes a difference whether you divide 5 by 2, or 2 by 5. For those operators you have to know the meaning of the argument order, but usually it will be rather obvious:
5 2 / means 5 divided by 2
2 5 ^ means 2 to the 5th power
If you are not familiar with RPN it may take a while to get used to it, but by now you have already learned all the rules there are!
When writing, or debugging, an RPN expression, always have this in mind:
- The expression must start with an argument (a number, unless it is a variable or a constant, but they, too, stand for numbers).
- The expression, unless it simply consists of one argument (in which case the argument is also the result), must end with an operator.
- As, from left to right, each operator replaces itself and its arguments with its result, all operators necessarily only have numbers to their left.
- When all operations have been performed, then exactly one argument must remain, which is the final result. If more than one argument remains, or if an operator finds itself short of the argument(s) it needs, then the expression is incorrect (comparable to an infix expression in which a closing parenthesis is missing, or there is one too many).
Compared with standard RPN, Hypatia not only offers a large number of additional 1- and 2-argument operators (a total of 89 of them!), it also knows n-argument operators (or statistical operators), which take a variable number of arguments: all the arguments they find to their left.
Example:
3 7 5 1 4 SUM
Since n-argument-operators can count the number of their arguments, they can calculate statistical parameters:
3 7 5 1 4 MEAN
gives the mean value 4, as the sum of the MEAN operator's arguments divided by their number.
N-argument-operators can be used with data read from a file, this will be discused in the chapter "Files".
For more information on Hypatia's operators, see the chapter "Operators".