Search                        Top                                  Index
HELP MOD                                      A. Sloman March 1987

<number> mod <number> -> <number>

MOD is an infix operator (precedence 2) which returns the "modulus" of
the first number with respect to the second. It is partly similar to
REM, which returns the remainder. REM is guaranteed to produce a result
with the same sign as its first argument, MOD with the same sign as its
second argument. So MOD and REM can give different results
for negative arguments:

    10 mod 3   =  1     (= 10 rem 3)

    -10 mod 3  =  2     -10 rem 3  = -1

    10 mod -3  = -2     10 rem -3  =  1

    -10 mod -3 = -1     (= -10 rem -3)

    15 mod -5  = -5     15 rem -5  =  0

REM will never return a negative result when its first argument is
positive or a positive (non-zero) result when its first argument is
negative. MOD returns a result in the interval between its second
argument and 0. It can return the lower but not the upper bound of
this interval.

The definition of mod is, in effect:

    define 2 x mod y -> result;
        lvars x y result;
        x // y -> -> result;
        if result < 0 then
            if y >= 0 then y + result else result endif
        elseif y < 0 then
            y + result
        else
            result
        endif;
    enddefine;

MOD can now be applied to all non-complex number types i.e. it is not
restricted to integers. So:

    67.8 mod 11.1 =>
    ** 1.2

    33_/8 mod 2 =>
    ** 1_/8

REM but not MOD can be applied to complex numbers.
    sqrt(-1) mod 5 =>
    ;;; MISHAP - REAL NUMBER(S) NEEDED
    ;;; INVOLVING:  0 0.0_+:1.0
    ;;; DOING    :  < mod compile

MOD needs to check its arguments. If you know they are going to be
integers, you can use the fast_integer version FI_MOD instead.
See REF * FASTPROCS and HELP * EFFICIENCY.

See
REF * NUMBERS
    for details of POP-11 number types and mathematical operations.

See also HELP
    * MATH - for a summary of mathematical operations POP-11
    * DIV  - division operator for integers only
    * REM  - remainder

-----<Copyright University of Sussex 1987.  All rights reserved.>-------