LDIV - system-independent long integer division.

(ANSI Standard)

Usage:

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

Where:

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

Description:

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

long quot;  /*quotient*/
long 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

ldiv_t x;
x = ldiv(9,5);
printf("%d\n",x.quot);   /* prints 1 */
x = ldiv(-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 div

Copyright © 1996, Thinkage Ltd.