Some of my Avisynth filters take RPN strings as inputs to perform calculations. For a general view of Reverse Polish Notation, see the Wikipedia page.
Documentation for my specific RPN implementation:
|x y +
|Add the top two operands on the stack
|x + y
|x y -
|Subtract y from x
|x - y
|x y *
|Multiply x by y
|x * y
|x y /
|Divide x by y
|x / y
|x y \
|Divide y by x
|y / x
|x y ^
|Raise x to the power of y. This is a slow operation; for raising to an integer power, use a combination of
|Square root of x
|x y %
|Remainder after dividing x by y
|x mod y
|Absolute value of x
|Negative value of x
|x y min
|Returns the lowest of x or y
|x y min
|Returns the highest of x or y
|x y zmax
|Constrains x between 0 and y
|Returns a random value between 0 and 1 (exclusive)
|Returns an integer random value between 0 (inclusive) and x (exclusive)
|Sine of x
|Cosine of x
|Tangent of x
|This operations places both the sine and cosine of x onto the stack. It is faster than performing the operations separately.
|Inverse sine of x
|Inverse cosine of x
|Inverse tangent of x
|x y atan2
|Inverse tangent of y/x (taking into account the signs of y and x)
|x y <
|True if x is less than y
|x < y
|x y >
|True if x is greater than y
|x > y
|Less than or equal
|x y <=
|True if x is less than or equal to y
|x <= y
|Greater than or equal
|x y >=
|True if x is greater than or equal toy
|x >= y
|x y ==
|True if x is equal to y
|x == y
|x y !=
|True if x is not equal to y
|x != y
|Duplicates the top item on the stack. This operation can be used in combination with
* to raise to integer powers.
|x y swap
|Swaps the top two items on the stack. Can be used, for example, in combination with
dup to apply multiple operations to the same input. For example
x dup asin swap acos will leave both the arcsine and arccosine of x on the stack.
The comparison operators above can be used with the conditional operator,
?, to return different values based on the result of the comparison (or other operation). The format is
[result_if_true] [result_if_false] [condition] ?
10 20 x y < ? will return a value of 10 if x is less than y, otherwise it will return 20.
A condition is considered true if the value is not zero, otherwise it is considered false.
There are no boolean operations, but these can be simulated using arithmetic operations.
Note that all calculations are performed even if the value is ultimately bypassed by a conditional operation. For example:
x y ^ 0 x y < ? - the calcuation of x^y will occur regardless of the outcome of the comparison.
As an extension to the general RPN concept, the implementation allows for saving and loading of user-defined and filter-defined variables. Variables are referenced by a single case-sensitive alphabetical character.
To store the current top value of the stack to a variable:
To store the current top value and pop it from the top of the stack:
To recall a variable, simply use the character:
q Example of adding two variables:
a b +
Some filters use variables to store specific values. These always use lower-case letters, so upper-case letters are always free to use.
Using an uninitialised value will return an uncertain value.