RPN

Some of my Avisynth filters take RPN strings as inputs to perform calculations. For an overview of Reverse Polish Notation, see the Wikipedia page.

Documentation for my specific RPN implementation:

Constants

The following constants are available:

Operations

Arithmetic operations
OperationOpExampleDescriptionResult
Add+x y +Add the top two operands on the stackx + y
Subtract-x y -Subtract y from xx - y
Multiply*x y *Multiply x by yx * y
Divide*x y /Divide x by yx / y
Reverse divide*x y \Divide y by xy / x
Power^x y ^Raise x to the power of y. This is a slow operation; for raising to an integer power, use a combination of dup and *x y
Square rootsqrtx sqrtSquare root of x√ x
Modulo%x y %Remainder after dividing x by yx mod y
Absoluteabsx absAbsolute value of xfabs(x)
Negatenegx negNegative value of x-x
Min/max
OperationOpExampleDescriptionResult
Minimumminx y minReturns the lowest of x or ymin(x,y)
Maximmummaxx y minReturns the highest of x or ymax(x,y)
Zero-maxzmaxx y zmaxConstrains x between 0 and ymax(0,min(x,y))
Random
OperationOpExampleDescriptionResult
Floating-point randomrandrandReturns a random value between 0 and 1 (exclusive)(0,1)
Integer randomirandx irandReturns an integer random value between 0 (inclusive) and x (exclusive)[0,x)
Trigonometrical operations
OperationOpExampleDescriptionResult
All trigonometrical operations use radians as the unit of angle
Sinesinx sinSine of xsin(x)
Cosinecosx cosCosine of xcos(x)
Tantanx tanTangent of xtan(x)
Sine/cosinesincosx sincosThis operations places both the sine and cosine of x onto the stack. It is faster than performing the operations separately.sin(x), cos(x)
Arcsineasinx asinInverse sine of xasin(x)
Arccosineacosx acosInverse cosine of xacos(x)
Atanatanx atanInverse tangent of xatan(x)
Atan2atan2x y atan2Inverse tangent of y/x (takes into account the signs of y and x)atan2(y, x)
Comparison operations
OperationOpExampleDescriptionResult
Comparison operations return 1 if true or 0 if false
Less than<x y <True if x is less than yx < y
Greater than>x y >True if x is greater than yx > y
Less than or equal<=x y <=True if x is less than or equal to yx <= y
Greater than or equal>=x y >=True if x is greater than or equal to yx >= y
Equal==x y ==True if x is equal to yx == y
Not equal!=x y !=True if x is not equal to yx != y
Bitwise operations
OperationOpExampleDescriptionResult
Bitwise operations reduce operands to integers by rounding down (floor) before applying the operation. They are not meant to be used as logical operators.
Andandx y andBitwise AND of floor(x) and floor(y)x & y
Ororx y orBitwise OR of floor(x) and floor(y)x | y
Xorxorx y xorBitwise XOR of floor(x) and floor(y)x ^ y
Notnotx notBitwise NOT of floor(x)~ x
Conditional operator
OperationOpExampleDescriptionResult
Conditional?x y z ?Returns y if z is equal to 0; otherwise returns x. Note that all components of x and y are calculated regardless of the eventual outcome of the conditional operator.if (z != 0) then x else y
Stack operations
OperationOpExampleDescriptionResult
Duplicatedupx dupDuplicates the top item on the stack. This operation can be used in combination with * to raise to integer powers.x, x
Swapswapx y swapSwaps 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.y, x

Conditional calculation

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] ?

For example: 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.

Variables

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: @q

To store the current top value and pop it from the top of the stack: @q^

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 variable will return an indeterminate value.