Skip to content

Arithmetic#

CEL supports standard arithmetic operations on numeric types.

Negation#

The unary - operator negates a numeric value.

-3.14
// result: -3.14 (double)
// input: x = 100
-x
// result: -100 (int)

Negation is not defined for unsigned integers.

-1u
// error: found no matching overload for '-_' applied to '(uint)'

Basic operations#

Addition, subtraction, multiplication, and division work on numeric types.

1 + 2
// result: 3 (int)
10u - 4u
// result: 6 (uint)
3.0 * 4.0
// result: 12 (double)

Integer division truncates toward zero.

15 / 4
// result: 3 (int)
-15 / 4
// result: -3 (int)

Modulo#

The % operator returns the remainder after division. The result has the same sign as the dividend.

17 % 5
// result: 2 (int)
-17 % 5
// result: -2 (int)
17 % -5
// result: 2 (int)

Operator precedence#

Multiplication, division, and modulo have higher precedence than addition and subtraction.

1 + 2 * 3
// result: 7 (int)
(1 + 2) * 3
// result: 9 (int)

Division by zero#

Integer and unsigned integer division by zero produces a runtime error.

1 / 0
// error: division by zero
1 % 0