Operators
Arithmetic operators
FullThe numeric infixes — + - / % * div mod — and the types they return.
The arithmetic operators work across the whole numeric tower. Division of integers yields an exact Rat (see Rational literals), not a truncated integer.
The basic set #
say 7 + 3; say 7 - 3; say 7 * 3; say 7 / 2; say 7 ** 2;
Output
10
4
21
3.5
49Modulus and integer division #
% is modulus, ** is exponentiation, and div/mod are the integer-only counterparts of / and %.
say 7 % 3; say 7 div 2; say 7 mod 3;
Output
1
3
1gcd and lcm #
Greatest common divisor and least common multiple are built-in infix operators.
say 12 gcd 18; say 4 lcm 6;
Output
6
12Notes #
/never truncates:7 / 2is3.5(aRat), and6 / 3is2but still aRat. Usedivfor floor integer division.%gives the mathematical modulo, taking the sign of the divisor:-7 % 3is2, not-1.- Exponentiation
**is right-associative:2 ** 2 ** 3is2 ** 8=256, not4 ** 3.