Mathematical Operations
Learn how to use mathematical operations in your programs.
Reference
| Name | Operation | Description | |
| Add/Concatenate | + | ( a b — a+b ) | Pops the last 2 items from the stack and pushes their sum (if numbers) or concatenation (if strings). |
| Subtract | - | ( a b — a-b ) | Pops the last 2 items from the stack and pushes their difference. |
| Multiply | * | ( a b — ab ) | Pops the last 2 items from the stack and pushes their product. |
| Divide | / | ( a b — a÷b ) | Pops the last 2 items from the stack and pushes their quotient. |
| Modulo | % | ( a b — a%b ) | Pops the last 2 items from the stack and pushes their modulo. |
| Exponent | ** | ( a b — ab ) | 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" + Print
# 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)