DIV - system-independent integer division.

(ANSI Standard)

Usage:

#include <stdlib.h>
result = div(top,bottom);

Where:

int top;
is the numerator of the division.
int bottom;
is the denominator of the division.
div_t result;
is a structure containing the result of the division (see below).

Description:

The "div" function performs the integer division of "top" divided by "bottom". The result of "div" has a structure type named "div_t", defined in <stdlib.h> with the members

int quot;  /*quotient*/
int rem;   /*remainder*/

The "quot" element is the integer quotient of the division and the "rem" element is the remainder. The remainder always has the same sign as the result of the division (which is not true of A%B in some implementations). The quotient is defined so that

top == quot*bottom + rem

When the division is inexact, this means that the quotient is always adjusted towards 0. Thus

div_t x;
x = div(9,5);
printf("%d\n",x.quot);   /* prints 1 */
x = div(-9,5);
printf("%d\n",x.quot);   /* prints -1 */

If the result of the division cannot be represented, the behavior is undefined.

See Also:

expl c lib ldiv

Copyright © 1996, Thinkage Ltd.