COHERENT manpages

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

List of available manpages
Index


div() -- General Function (libc)

Perform integer division
#include <stdlib.h>
div_t div(numerator, denominator)
int numerator, denominator;

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

    typedef struct {
        int quot;
        int rem;
    } div_t;

div() 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

ldiv(),
libc,
stdlib.h
ANSI Standard, §7.10.6.2

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 div() is undefined.  Caveat utilitor.