COHERENT manpages

This page displays the COHERENT manpage for ldiv() [Perform long integer division].

List of available manpages
Index


ldiv() -- General Function (libc)

Perform long integer division
#include <stdlib.h>
ldiv_t ldiv(numerator, denominator)
long numerator, denominator;

ldiv() divides numerator by denominator. It returns a structure of the type
ldiv_t, which is structured as follows:

    typedef struct {
        long quot;
        long rem;
    } ldiv_t;

ldiv() writes the quotient into quot and the remainder into rem.

The sign of the quotient is  positive if the signs of the arguments are the
same; it is negative if the signs of the arguments differ.  The sign of the
remainder is the same as the sign of the numerator.

If the remainder is non-zero, the  magnitude of the quotient is the largest
integer less  than the  magnitude of the  algebraic quotient.  This  is not
guaranteed  by the  operators /  and %,  which merely  do what  the machine
implements for divide.

See Also

libc
ANSI Standard, §7.10.6.4

Notes

The ANSI  Standard includes this function to permit  a useful feature found
in most  versions of FORTRAN, where  the sign of the  remainder will be the
same  as the  sign  of the  numerator.   Also, on  most machines,  division
produces a remainder.  This allows  a quotient and remainder to be returned
from one machine-divide operation.

If the result of  division cannot be represented (e.g., because denominator
is set to zero), the behavior of ldiv() is undefined.  Caveat utilitor.