Numeric Operators

Numeric operators generally follow C styles.

Unary Operators

OperatorDescription
+positive
-negative
let number = +42;

number = -5;

number = -5 - +5;

-(-42) == +42;      // two '-' equals '+'
                    // beware: '++' and '--' are reserved symbols

Binary Operators

OperatorDescriptionResult typeINTFLOATDecimal
+, +=plusnumericyesyes, also with INTyes, also with INT
-, -=minusnumericyesyes, also with INTyes, also with INT
*, *=multiplynumericyesyes, also with INTyes, also with INT
/, /=divide (integer division if acting on integer types)numericyesyes, also with INTyes, also with INT
%, %=modulo (remainder)numericyesyes, also with INTyes, also with INT
**, **=power/exponentiationnumericyesyes, also FLOAT**INTno
<<, <<=left bit-shiftnumericyesnono
>>, >>=right bit-shiftnumericyesnono
&, &=bit-wise Andnumericyesnono
|, |=bit-wise Ornumericyesnono
^, ^=bit-wise Xornumericyesnono
==equals toboolyesyes, also with INTyes, also with INT
!=not equals toboolyesyes, also with INTyes, also with INT
>greater thanboolyesyes, also with INTyes, also with INT
>=greater than or equals toboolyesyes, also with INTyes, also with INT
<less thanboolyesyes, also with INTyes, also with INT
<=less than or equals toboolyesyes, also with INTyes, also with INT
..exclusive rangerangeyesnono
..=inclusive rangerangeyesnono

Examples

let x = (1 + 2) * (6 - 4) / 2;  // arithmetic, with parentheses

let reminder = 42 % 10;         // modulo

let power = 42 ** 2;            // power

let left_shifted = 42 << 3;     // left shift

let right_shifted = 42 >> 3;    // right shift

let bit_op = 42 | 99;           // bit masking

Floating-Point Interoperates with Integers

When one of the operands to a binary arithmetic operator is floating-point, it works with INT for the other operand and the result is floating-point.

let x = 41.0 + 1;               // 'FLOAT' + 'INT'

type_of(x) == "f64";            // result is 'FLOAT'

let x = 21 * 2.0;               // 'FLOAT' * 'INT'

type_of(x) == "f64";

(x == 42) == true;              // 'FLOAT' == 'INT'

(10 < x) == true;               // 'INT' < 'FLOAT'

Decimal Interoperates with Integers

When one of the operands to a binary arithmetic operator is Decimal, it works with INT for the other operand and the result is Decimal.

let d = parse_decimal("2");

let x = d + 1;                  // 'Decimal' + 'INT'

type_of(x) == "decimal";        // result is 'Decimal'

let x = 21 * d;                 // 'Decimal' * 'INT'

type_of(x) == "decimal";

(x == 42) == true;              // 'Decimal' == 'INT'

(10 < x) == true;               // 'INT' < 'Decimal'

Unary Before Binary

In Rhai, unary operators take precedence over binary operators. This is especially important to remember when handling operators such as ** which in some languages bind tighter than the unary - operator.

-2 + 2 == 0;

-2 - 2 == -4;

-2 * 2 == -4;

-2 / 2 == -1;

-2 % 2 == 0;

-2 ** 2 = 4;            // means: (-2) ** 2
                        // in some languages this means: -(2 ** 2)