Basic Operations
Introduction
In stack-based programming with Reverse Polish Notation (RPN), operations are performed by manipulating a stack data structure. RPN expressions are written in postfix notation, where operators follow their operands. This allows for efficient calculations without the need for parentheses or precedence rules.
Mathematical Operations
Addition (+)
The + operator adds two numbers by popping them from the the stack, performing the addition operation, and pushing the result back onto the stack. Both integer and floating-point numbers are supported. If at least one of the operands is a floating-point number, the result will be a floating-point number.
Example:
5 3 + => 8.0
Subtraction (-)
The - operator subtracts the first value on the stack from the second value by popping them from the stack, performing the subtraction operation, and pushing the result back onto the stack. Both integer and floating-point numbers are supported. If at least one of the operands is a floating-point number, the result will be a floating-point number.
Example:
5 3 - => 2.0
Multiplication (*)
The * operator multiplies two numbers by popping them from the the stack, performing the multiplication operation, and pushing the result back onto the stack. Both integer and floating-point numbers are supported. If at least one of the operands is a floating-point number, the result will be a floating-point number.
Example:
5 3 * => 15.0
Division (/)
The / operator divides the second value on the stack by the first value by popping them from the stack, performing the division operation, and pushing the result back onto the stack. Both integer and floating-point numbers are supported. The result will always be a floating-point number.
Example:
11 5 / => 2
Modulus (%)
The % operator calculates the remainder of dividing the second value on the stack by the first value by popping them from the stack, performing the modulus operation, and pushing the result back onto the stack. This operation is only supported for integer numbers.
Example:
10 3 % => 1
Exponential (pow)
The pow operator raises the second value on the stack to the power of the first value by popping them from the stack, performing the exponentiation operation, and pushing the result back onto the stack. Both integer and floating-point numbers are supported. The result will always be a floating-point number.
Example:
2 3 pow => 8.0
Square Root (sqrt)
The sqrt compute the square root of the first value in the stack and pushes the result back onto the stack. Both integer and floating-point numbers are supported. The result will always be a floating-point number.
Example:
9 sqrt => 3.00000
Boolean Operations
Logical AND (and)
The and operator performs a logical AND operation between the first two boolean values on the stack. It pops the two values from the stack, performs the logical AND operation, and pushes the result back onto the stack.
Example:
true true and => true
true false and => false
Logical OR (or)
The or operator performs a logical OR operation between the first two boolean values on the stack. It pops the two values from the stack, performs the logical OR operation, and pushes the result back onto the stack.
Example:
true false or => true
false false or => false
Logical XOR (xor)
The xor operator performs a logical XOR operation between the first two boolean values on the stack. It pops the two values from the stack, performs the logical XOR operation, and pushes the result back onto the stack.
Example:
true true xor => false
true false xor => true
Logical NOT (not)
The not operator performs a logical NOT operation on the first boolean value on the stack. It pops the value from the stack, performs the logical NOT operation, and pushes the result back onto the stack.
Example:
true not => false
false not => true
Comparison Operations
Equal (==)
The == operator compares the first two values on the stack for equality. It pops two values from the stack, compares them, and pushes true if they are equal, otherwise pushes false.
Example:
5 5 == => true
5 6 == => false
Not Equal (!=)
The != operator compares the first two values on the stack for inequality. It pops the two values from the stack, compares them, and pushes true if they are not equal, otherwise pushes false.
Example:
5 5 != => false
5 6 != => true
Less Than (<)
The < operator compares the first two values on the stack. It pops the two values from the stack, compares them, and pushes true if the second value is greater than the first, otherwise pushes false.
Example:
5 6 < => true
6 5 < => false
5 5 < => false
Greater Than (>)
The > operator compares the first two values on the stack. It pops the two values from the stack, compares them, and pushes true if the first value is greater than the second, otherwise pushes false.
Example:
5 6 > => false
6 5 > => true
5 5 > => false
Less Than or Equal To (<=)
The <= operator compares the first two values on the stack. It pops the two values from the stack, compares them, and pushes true if the second value is greater than or equal to the first, otherwise pushes false.
Example:
5 6 <= => true
6 5 <= => false
5 5 <= => true
Greater Than or Equal To (>=)
The >= operator compares the first two values on the stack. It pops the two values from the stack, compares them, and pushes true if the first value is greater than or equal to the second, otherwise pushes false.
Example:
5 6 >= => false
6 5 >= => true
5 5 >= => true
Type Casting
Integer Casting (int)
The int operator converts a floating-point number on the stack to an integer by truncating the decimal part. It pops a value from the stack, performs the conversion, and pushes the integer result back onto the stack.
Example:
5.7 int => 5
3.14 int => 3
Exit Operation
Exit (exit)
The exit operation terminates the execution of the program.