Arithmetic Operators¶
Mathematical operators for numeric computations.
Operators¶
| Operator | Description | Example |
|---|---|---|
+ |
Addition | u.age + 1 |
- |
Subtraction | u.balance - 10 |
* |
Multiplication | u.hours * u.rate |
/ |
Division | u.total / u.count |
% |
Modulo (remainder) | u.id % 10 |
^ |
Exponentiation | 2 ^ 8 |
+ |
Unary plus | +5 |
- |
Unary minus | -5 |
Examples¶
Basic Arithmetic¶
-- Calculate age plus one
RETURN u.age + 1 AS next_age
-- Calculate total from price and tax
RETURN p.price + (p.price * p.tax_rate) AS total
Division¶
-- Average calculation (integer division)
RETURN total_amount / item_count AS average
-- For precise division, use FLOAT64
RETURN CAST(total_amount AS FLOAT64) / item_count
Modulo¶
Exponentiation¶
Operator Precedence¶
Precedence (highest to lowest):
1. Unary +, unary -
2. ^
3. *, /, %
4. +, -
Use parentheses for clarity:
Type Promotion¶
Operations return appropriate types:
| Operation | Operand Types | Result Type |
|---|---|---|
+, -, * |
INT64, INT64 | INT64 |
+, -, * |
INT64, FLOAT64 | FLOAT64 |
+, -, * |
FLOAT64, FLOAT64 | FLOAT64 |
/ |
any | FLOAT64 |
% |
INT64, INT64 | INT64 |
^ |
any | FLOAT64 |
Overflow¶
Arithmetic overflow wraps around:
For large numbers, consider FLOAT64:
NULL Handling¶
Any arithmetic operation with NULL returns NULL:
Common Patterns¶
Percentage Calculation¶
Currency Conversion¶
Age Calculation¶
See Also¶
- Math Functions — ABS, CEIL, FLOOR, etc.
- Comparison Operators — Comparing numeric values