DIV - integer divide with uniform direction of truncation.

Alternate Entry Name: .DIV

REM - obtain the associated (always positive) remainder.

Alternate Entry Name: .REM

Usage:

B:
   quotient  = div( dividend, divisor );
   remainder = rem( dividend, divisor );
C:
   #equate _div .div
   #equate _rem .rem
   int _div(int dividend, int divisor);
   int _rem(int dividend, int divisor);

Description:

DIV returns as its value the quotient which is obtained when "dividend" is divided by "divisor" in such a way as to leave a remainder that is greater than or equal to zero. The relation

A == div(A,B)*B + rem(A,B)

is always true. Since the remainder is always greater than or equal to zero, inexact divisions result in quotients that are always truncated downwards, except for divisions of a negative number by a negative number where the quotient is adjusted upward. The examples at the end of this description will help to clarify this process.

The function REM returns the remainder left when division is performed in this fashion. This will always be greater than or equal to zero.

In contrast, using the "/" operator in B causes the result to be truncated towards zero. The "%" operator will give a negative result if its left operand is negative.

The following table indicates results obtained by calling DIV and REM.

a     b     a/b    a%b    div(a,b)  rem(a,b)
-7    3     -2     -1       -3         2
 7    3      2      1        2         1
-7   -3      2     -1        3         2
 7   -3     -2      1       -2         1

Notes:

This DIV function differs from the standard C "div" function. The C "div" function always truncates toward zero; this DIV function always truncates toward negative infinity.

Copyright © 1996, Thinkage Ltd.