.TH "ldiv()" "" "" "General Function (libc)"
.PC "Perform long integer division"
.B "#include <stdlib.h>"
\fBldiv_t ldiv(\fInumerator\^\fB, \fIdenominator\^\fB)\fR
\fBlong \fInumerator\^\fB, \fIdenominator\^\fB;\fR
.PP
.II ldiv_t
.II "division, integer"
.II "integer division"
.B ldiv()
divides
.I numerator
by
.IR denominator .
It returns a structure of the type
.BR ldiv_t ,
which is structured as follows:
.DM
	typedef struct {
		long quot;
		long rem;
	} ldiv_t;
.DE
.PP
.B ldiv()
writes the quotient into
.B quot
and the remainder into
.BR rem .
.PP
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.
.PP
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
.B /
and
.BR % ,
which merely do what the machine implements for divide.
.SH "See Also"
.Xr "libc" libc
.br
\*(AS, \(sc7.10.6.4
.SH Notes
The \*(AS 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.
.PP
If the result of division cannot be represented (e.g., because
.I denominator
is set to zero), the behavior of
.B ldiv()
is undefined.
.I "Caveat utilitor."
