[ Arithmetic | The ECLiPSe Built-In Predicates | Reference Manual | Alphabetic Index ]

# mod(+Number1, +Number2, -Result)

Evaluates the modulus Number1 mod Number2 and unifies the resulting value with Result.

Number1
  Integer.
Number2
  Integer.
Result
  A variable or integer.

## Description

This predicate is used by the ECLiPSe compiler to expand evaluable arithmetic expressions. So the call to mod(Number1, Number2, Result) is equivalent to

        Result is Number1 mod Number2

which should be preferred for portability.

The modulus operation computes the remainder corresponding to the flooring division div. The following relation always holds:

        X =:= (X mod Y) + (X div Y) * Y.

The result Result is either zero, or has the same sign as Number2.

In coroutining mode, if Number1 or Number2 are uninstantiated, the call to mod/3 is delayed until these variables are instantiated.

CAUTION: The behaviour of mod was changed for standard compliance! In ECLiPSe versions up to 5.8, mod computed the remainder corresponding to the truncating division //, and thus gave different results for arguments with opposite signs. Moreover, the operator precedence was changed from op(300,xfx,mod) to op(400,yfx,mod), which means that a*b mod c is now parsed as (a*b)mod c rather than a*(b mod c).

### Modes and Determinism

- mod(+, +, -) is det

### Exceptions

(4) instantiation fault
  Number1 or Number2 is not instantiated (non-coroutining mode only).
(5) type error
  Result is a number but not an integer.
(5) type error
  Number1 or Number2 is a number but not an integer.
(20) arithmetic exception
  Illegal arithmetic operation: Number2 is zero
(24) number expected
  Number1 or Number2 is not of a numeric type.
(24) number expected
  Result is neither a number nor a variable.

## Examples

    Success:
          X is 10 mod 3.		(gives X = 1)

          mod( 10,  3,  1).
          mod(-10,  3,  2).
          mod( 10, -3, -2).
          mod(-10, -3,  1).

          mod( 11,  3,  2).
    Fail:
          mod(1, 2, 3).
    Error:
          mod(A, 2, 6).              (Error 4).
          mod(6, 2.0, 3.0).          (Error 5).
          mod(2, 0, Result).         (Error 20).
          mod(4 + 2, 2, 12).         (Error 24).
          mod(5, 2, r).              (Error 24).

## See Also

is / 2, div / 3, rem / 3