3 <<div>>---divide two integers
10 div_t div(int <[n]>, int <[d]>);
20 returning quotient and remainder as two integers in a structure <<div_t>>.
23 The result is represented with the structure
31 where the <<quot>> field represents the quotient, and <<rem>> the
32 remainder. For nonzero <[d]>, if `<<<[r]> = div(<[n]>,<[d]>);>>' then
33 <[n]> equals `<<<[r]>.rem + <[d]>*<[r]>.quot>>'.
35 To divide <<long>> rather than <<int>> values, use the similar
41 No supporting OS subroutines are required.
45 * Copyright (c) 1990 Regents of the University of California.
46 * All rights reserved.
48 * This code is derived from software contributed to Berkeley by
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
54 * 1. Redistributions of source code must retain the above copyright
55 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
59 * 3. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 #include <stdlib.h> /* div_t */
88 * The ANSI standard says that |r.quot| <= |n/d|, where
89 * n/d is to be computed in infinite precision. In other
90 * words, we should always truncate the quotient towards
91 * 0, never -infinity or +infinity.
93 * Machine division and remainer may work either way when
94 * one or both of n or d is negative. If only one is
95 * negative and r.quot has been truncated towards -inf,
96 * r.rem will have the same sign as denom and the opposite
97 * sign of num; if both are negative and r.quot has been
98 * truncated towards -inf, r.rem will be positive (will
99 * have the opposite sign of num). These are considered
102 * If both are num and denom are positive, r will always
105 * This all boils down to:
106 * if num >= 0, but r.rem < 0, we got the wrong answer.
107 * In that case, to get the right answer, add 1 to r.quot and
108 * subtract denom from r.rem.
109 * if num < 0, but r.rem > 0, we also have the wrong answer.
110 * In this case, to get the right answer, subtract 1 from r.quot and
111 * add denom to r.rem.
113 if (num
>= 0 && r
.rem
< 0) {
117 else if (num
< 0 && r
.rem
> 0) {