Threadon / Operators

Operators

arithmetic, comparison, and logic

Arithmetic

OperatorWhat it doesExampleResult
+add2 + 35
-subtract7 - 25
*multiply4 * 312
/dividesee belowsee below
//floor divide7 // 23
%remainder7 % 21
**power2 ** 38

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
4

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
a: 7 b: 3 2 2 1

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
4.000000 3.500000

Comparison

OperatorWhat 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
True False True True

Boolean operators

Three keywords combine bools. Both operands must be bools.

OperatorWhat it does
andtrue only if both sides are true
ortrue if either side is true
notflips true to false and back
def main() -> Int32
    b: Bool = False
    print(not b)
    print(not b == b)
    return 0
True False

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:

LevelOperators
1or
2and
3not
4== != < > <= >=
5+ -
6* / // %
7** (right to left)
8unary - +
9function 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
14 20

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.