(ANSI Standard)
#include <stdlib.h> result = ldiv(top,bottom);
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.
Copyright © 1996, Thinkage Ltd.