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 typeIntegerFloating-pointDecimal
+, +=plusnumericyesyes, also integeryes, also integer
-, -=minusnumericyesyes, also integeryes, also integer
*, *=multiplynumericyesyes, also integeryes, also integer
/, /=divide (integer division if acting on integer types)numericyesyes, also integeryes, also integer
%, %=modulo (remainder)numericyesyes, also integeryes, also integer
**, **=power/exponentiationnumericyesyes, also FLOAT**INTno
<<, <<=left bit-shiftnumericyesnono
>>, >>=right bit-shiftnumericyesnono
&, &=bit-wise Andnumericyesnono
|, |=bit-wise Ornumericyesnono
^, ^=bit-wise Xornumericyesnono
==equals toboolyesyes, also integeryes, also integer
!=not equals toboolyesyes, also integeryes, also integer
>greater thanboolyesyes, also integeryes, also integer
>=greater than or equals toboolyesyes, also integeryes, also integer
<less thanboolyesyes, also integeryes, also integer
<=less than or equals toboolyesyes, also integeryes, also integer
..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 integer for the other operand and the result is floating-point.

let x = 41.0 + 1;               // float + integer

type_of(x) == "f64";            // result is float

let x = 21 * 2.0;               // float * integer

type_of(x) == "f64";

(x == 42) == true;              // float == integer

(10 < x) == true;               // integer < float

Decimal Interoperates with Integers

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

let d = parse_decimal("2");

let x = d + 1;                  // decimal + integer

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

let x = 21 * d;                 // decimal * integer

type_of(x) == "decimal";

(x == 42) == true;              // decimal == integer

(10 < x) == true;               // integer < 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)