September 2017
Beginner
402 pages
9h 52m
English
The % is the modulo operator. It returns the remainder of the division of its operands, as shown here:
say 100 % 3; # 1say 10 % 3; # 1say 5 % 3; # 2
The result of the modulo operation $a % $b is equivalent to the following wordy expression:
$a - $b * floor($a / $b);
Here, floor is the function that rounds the value down. Take one of the preceding examples—10 % 3. The result of it means that 3 can be subtracted from 10 a few times until 1 remains, which is less than 3 and thus cannot be decreased by it.
Traditionally, the modulo operator is used with integer operands, but it still works fine with rational and floating-point numbers. The values of these types can be accepted by the % operator without type casts. Let's examine ...