% (modulus) operator

Type Assocation Precedence Input types Output types
binary left 5 int
float
int
float

This operator divides the first operand by the second operand and returns the remainder. The type of the result is equal to the largest type of the operands (float is larger than int). The return value is the remainder obtained by dividing both operands:

a % b = a – trunc(a/b) * b

The sign of the return value is equal to the sign of a. Examples:

  5 % 4      ; 1  7 % 4      ; 3  4 % 4      ; 0  5.1 % 4    ; 1.1  4.3 % 1.5  ; 1.3  4.5 % 1.5  ; 0  -8 % 5     ; -3  8 % -5     ; 3  -8 % -5    ; -3    

Note: the second operand should not be equal to zero. Otherwise, the value 0 is returned.

See Also
/ (division) operator

% (modulus) operator