Skip to content

Operators

Zeus provides a familiar set of operators for arithmetic, comparison, and logical operations.

Arithmetic Operators

OperatorDescriptionExample
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division15 / 35

Examples

function main(): i32 {
let a: i32 = 10;
let b: i32 = 3;
let sum: i32 = a + b; // 13
let diff: i32 = a - b; // 7
let product: i32 = a * b; // 30
let quotient: i32 = a / b; // 3 (integer division)
return sum + diff + product + quotient;
}

Integer Division

Division between integers produces an integer result:

let result: i32 = 7 / 2; // 3, not 3.5

For floating-point division, use float types:

let result: f64 = 7.0 / 2.0; // 3.5

Unary Minus

Negate a value:

let positive: i32 = 42;
let negative: i32 = -positive; // -42

Comparison Operators

OperatorDescriptionExample
==Equal to5 == 5true
!=Not equal to5 != 3true
<Less than3 < 5true
<=Less than or equal5 <= 5true
>Greater than5 > 3true
>=Greater than or equal5 >= 5true

Examples

function compare(a: i32, b: i32): i32 {
if (a == b) {
return 0; // Equal
}
if (a < b) {
return -1; // a is smaller
}
return 1; // a is larger
}
function main(): i32 {
return compare(10, 5); // Returns 1
}

Comparing Different Types

Comparison works between compatible types:

let intVal: i32 = 5;
let floatVal: f64 = 5.0;
// This works because i32 can be implicitly converted to f64
let equal: boolean = floatVal == intVal; // true

Logical Operators

OperatorDescriptionExample
!Logical NOT!truefalse

Logical NOT

function main(): i32 {
let isReady: boolean = true;
let notReady: boolean = !isReady; // false
if (!notReady) {
return 1; // This executes
}
return 0;
}

Workaround for AND/OR

Until && and || are available, use nested conditions:

// Instead of: if (a > 0 && b > 0)
function bothPositive(a: i32, b: i32): boolean {
if (a > 0) {
if (b > 0) {
return true;
}
}
return false;
}
// Instead of: if (a > 0 || b > 0)
function eitherPositive(a: i32, b: i32): boolean {
if (a > 0) {
return true;
}
if (b > 0) {
return true;
}
return false;
}

Assignment Operator

The = operator assigns a value to a variable:

let x: i32 = 10; // Initial assignment
x = 20; // Reassignment
x = x + 5; // Update based on current value

Operator Precedence

Operators follow standard mathematical precedence:

PrecedenceOperatorsDescription
Highest.Member access
()Function call
!, - (unary)Logical NOT, negation
*, /Multiplication, division
+, -Addition, subtraction
<, <=, >, >=Comparison
==, !=Equality
Lowest=Assignment

Examples

let result: i32 = 2 + 3 * 4; // 14 (not 20)
let result2: i32 = (2 + 3) * 4; // 20 (parentheses first)
let result3: i32 = 10 - 5 - 2; // 3 (left to right)

Practical Examples

Absolute Value

function abs(n: i32): i32 {
if (n < 0) {
return -n;
}
return n;
}

Distance Formula (simplified)

function manhattanDistance(x1: i32, y1: i32, x2: i32, y2: i32): i32 {
let dx: i32 = x2 - x1;
let dy: i32 = y2 - y1;
if (dx < 0) {
dx = -dx;
}
if (dy < 0) {
dy = -dy;
}
return dx + dy;
}

Min and Max

function min(a: i32, b: i32): i32 {
if (a < b) {
return a;
}
return b;
}
function max(a: i32, b: i32): i32 {
if (a > b) {
return a;
}
return b;
}
function clamp(value: i32, minVal: i32, maxVal: i32): i32 {
return max(minVal, min(value, maxVal));
}

Next

Modules →