Arithmetic
| Operator | What it does | Example | Result |
|---|---|---|---|
+ | add | 2 + 3 | 5 |
- | subtract | 7 - 2 | 5 |
* | multiply | 4 * 3 | 12 |
/ | divide | see below | see below |
// | floor divide | 7 // 2 | 3 |
% | remainder | 7 % 2 | 1 |
** | power | 2 ** 3 | 8 |
Division deserves its own section. Read on.
Division
If the compiler can figure out both operands ahead of time, it folds the division at compile time.
def main() -> Int32
print(8 / 2)
return 0
integer division truncates to 0 it doesn't produce a float as answer nor rounds it up.
def main() -> Int32
a: Int32 = Int32(input("a: "))
b: Int32 = Int32(input("b: "))
print(a / b)
print(a // b)
print(a % b)
return 0
That run typed 7 and 3. 7 / 2 gave 3, 7 // 2 gave 3, 7 % 2 gave 1
Float division
def main() -> Int32
print(8.0 / 2.0)
print(7.0 / 2.0)
return 0
Comparison
| Operator | What it does |
|---|---|
== | equal to |
!= | not equal to |
< | less than |
> | greater than |
<= | less than or equal |
>= | greater than or equal |
Comparisons return a Bool. Both sides must be the same type. Comparing an Int32 to a Float32 is a compile error, even if the values look equal.
def main() -> Int32
print(3 < 5)
print(3 == 5)
print(3 != 5)
print(5 <= 5)
return 0
Boolean operators
Three keywords combine bools. Both operands must be bools.
| Operator | What it does |
|---|---|
and | true only if both sides are true |
or | true if either side is true |
not | flips true to false and back |
def main() -> Int32
b: Bool = False
print(not b)
print(not b == b)
return 0
A number is not a bool. if 1: won't compile, and neither will True and 1. Convert first with Bool(x).
Unary operators
Unary minus and plus work on numbers:
n: Int32 = -5
print(-n) # 5
print(+n) # -5, plus does nothing
They only work on Ints and Floats. Applying - to a bool or a string is a compile error.
The reference operator
The ^ operator, written after a variable, makes a reference to it. It's the closest thing Threadon has to pointers.
r: Int32 = x^
It deserves its own page: References.
What's not here
There are no bitwise operators. No &, |, ~, <<, or >>. There's no way to manipulate individual bits. That's on the Limits page.
Operator precedence
From weakest to strongest binding:
| Level | Operators |
|---|---|
| 1 | or |
| 2 | and |
| 3 | not |
| 4 | == != < > <= >= |
| 5 | + - |
| 6 | * / // % |
| 7 | ** (right to left) |
| 8 | unary - + |
| 9 | function calls, casts, parentheses, field access |
So 2 + 3 * 4 is 2 + (3 * 4), which is 14. And not a and b means (not a) and b.
def main() -> Int32
print(2 + 3 * 4)
print((2 + 3) * 4)
return 0
Two limits on **: chained powers like 2 ** 3 ** 2 are rejected, and a negative exponent doesn't work. Keep your exponents as plain positive numbers.
Operands must match
Both sides of +, -, *, /, //, %, **, and every comparison must be the same type. Mixing an Int32 with a Float32 is a compile error:
x: Float32 = 1 + 2.5 # compile error: Type mismatch
Convert one side first with a cast, or write literals that already match.