Mathematical Operations
Learn how to use mathematical operations in your programs.
Reference
Name | Operation | Description |
Add/Concatenate | + | Pops the last 2 items from the stack and pushes their sum (if numbers) or concatenation (if strings). |
Subtract | - | Pops the last 2 items from the stack and pushes their difference. |
Multiply | * | Pops the last 2 items from the stack and pushes their product. |
Divide | / | Pops the last 2 items from the stack and pushes their quotient. |
Modulo | % | Pops the last 2 items from the stack and pushes their modulo. |
Exponent | ** | Pops the last 2 items from the stack and exponentiates the numbers. |
Usage
Use the mathematical operations to perform simple calculations in your programs. Here are a few examples:
# Adding two numbers
10 10 + # The stack now contains: 20
# Concatenating strings
"Pile " "Programming Language" + println
# Output: Pile Programming Language
# Note: Concatenation works only with strings, not numbers.
# A more complex expression
2 50 10 * / 1 +
# Explanation:
# - Pushes 2, 50, and 10 onto the stack
# - Multiplies 50 and 10 (result: 500)
# - Divides 500 by 2 (result: 250)
# - Adds 1 to the result (final result: 251)