GNU Astronomy Utilities


Next: , Previous: , Up: Arithmetic   [Contents][Index]


6.2.1 Reverse polish notation

The most common notation for arithmetic operations is the infix notation where the operator goes between the two operands, for example \(4+5\). While the infix notation is the preferred way in most programming languages, currently Arithmetic does not use it since it will require parenthesis which can complicate the implementation of the code. In the near future we do plan to adopt this notation58, but for the time being (due to time constraints on the developers), Arithmetic uses the post-fix or reverse polish notation. The Wikipedia article provides some excellent explanation on this notation but here we will give a short summary here for self-sufficiency.

In the post-fix notation, the operator is placed after the operands, as we will see below this removes the need to define parenthesis for most ordinary operators. For example, instead of writing 5+6, we write 5 6 +. To easily understand how this notation works, you can think of each operand as a node in a first-in-first-out stack. Every time an operator is confronted, it pops the number of operands it needs from the top of the stack (so they don’t exist in the stack any more), does its operation and pushes the result back on top of the stack. So if you want the average of 5 and 6, you would write: 5 6 + 2 /. The operations that are done are:

  1. 5 is an operand, so it is pushed to the top of the stack.
  2. 6 is an operand, so it is pushed to the top of the stack.
  3. + is a binary operator, so pull the top two elements of the stack and perform addition on them (the order is \(5+6\) in the example above). The result is 11, push it on top of the stack.
  4. 2 is an operand so push it onto the top of the stack.
  5. / is a binary operator, so pull out the top two elements of the stack (top-most is 2, then 11) and divide the second one by the first.

In the Arithmetic program, the operands can be FITS images or numbers. As you can see, very complicated procedures can be created without the need for parenthesis or worrying about precedence. Even functions which take an arbitrary number of arguments can be defined in this notation. This is a very powerful notation and is used in languages like Postscript 59 (the programming language in Postscript and compiled into PDF files) uses this notation.


Footnotes

(58)

https://savannah.gnu.org/task/index.php?13867

(59)

See the EPS and PDF part of Recognized file formats for a little more on the Postscript language.


Next: , Previous: , Up: Arithmetic   [Contents][Index]