1 /*
2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
5
6 /*
7 * Copyright (c) 1990, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * Chris Torek.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42 #include "lint.h"
43 #include <sys/types.h>
44 #include <stdlib.h>
45
46 div_t
div(num,denom)47 div(num, denom)
48 int num, denom;
49 {
50 div_t r;
51
52 r.quot = num / denom;
53 r.rem = num % denom;
54 /*
55 * The ANSI standard says that |r.quot| <= |n/d|, where
56 * n/d is to be computed in infinite precision. In other
57 * words, we should always truncate the quotient towards
58 * 0, never -infinity.
59 *
60 * Machine division and remainer may work either way when
61 * one or both of n or d is negative. If only one is
62 * negative and r.quot has been truncated towards -inf,
63 * r.rem will have the same sign as denom and the opposite
64 * sign of num; if both are negative and r.quot has been
65 * truncated towards -inf, r.rem will be positive (will
66 * have the opposite sign of num). These are considered
67 * `wrong'.
68 *
69 * If both are num and denom are positive, r will always
70 * be positive.
71 *
72 * This all boils down to:
73 * if num >= 0, but r.rem < 0, we got the wrong answer.
74 * In that case, to get the right answer, add 1 to r.quot and
75 * subtract denom from r.rem.
76 */
77 if (num >= 0 && r.rem < 0) {
78 r.quot++;
79 r.rem -= denom;
80 }
81 return (r);
82 }
83
84 ldiv_t
ldiv(num,denom)85 ldiv(num, denom)
86 long num, denom;
87 {
88 ldiv_t r;
89
90 /* see div() for comments */
91
92 r.quot = num / denom;
93 r.rem = num % denom;
94 if (num >= 0 && r.rem < 0) {
95 r.quot++;
96 r.rem -= denom;
97 }
98 return (r);
99 }
100