Mod

From Free Pascal wiki
Revision as of 17:56, 1 March 2013 by Olaf (talk | contribs)
Jump to navigationJump to search

Deutsch (de) English (en) français (fr)

Mod (modulus) divides two numbers and returns only the remainder that is a whole number. For instance, the expression "a:= 13 mod 4;" would evaluate to 1 (a=1), while "b := 12 mod 4;" would evaluate to 0 (b=0).

From the language reference:

The sign of the result of a Mod operator is the same as the sign of the left side operand of the Mod operator. In fact, the Mod operator is equivalent to the following operation :
 I mod J = I - (I div J) * J 

For example "c := -13 mod 4;" results in c = -1.


But — this is what Delphi does. The ISO 7185 Pascal standard states:

Evaluation of a term of the form x mod y is an error if y is less than or equal to zero; otherwise there is an integer k such that x mod y satisfies the following relation :
 0 <= x mod y = x - k * y < y.