1 | /* |
---|
2 | FUNCTION |
---|
3 | <<div>>---divide two integers |
---|
4 | |
---|
5 | INDEX |
---|
6 | div |
---|
7 | |
---|
8 | SYNOPSIS |
---|
9 | #include <stdlib.h> |
---|
10 | div_t div(int <[n]>, int <[d]>); |
---|
11 | |
---|
12 | DESCRIPTION |
---|
13 | Divide |
---|
14 | @tex |
---|
15 | $n/d$, |
---|
16 | @end tex |
---|
17 | @ifnottex |
---|
18 | <[n]>/<[d]>, |
---|
19 | @end ifnottex |
---|
20 | returning quotient and remainder as two integers in a structure <<div_t>>. |
---|
21 | |
---|
22 | RETURNS |
---|
23 | The result is represented with the structure |
---|
24 | |
---|
25 | . typedef struct |
---|
26 | . { |
---|
27 | . int quot; |
---|
28 | . int rem; |
---|
29 | . } div_t; |
---|
30 | |
---|
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>>'. |
---|
34 | |
---|
35 | To divide <<long>> rather than <<int>> values, use the similar |
---|
36 | function <<ldiv>>. |
---|
37 | |
---|
38 | PORTABILITY |
---|
39 | <<div>> is ANSI. |
---|
40 | |
---|
41 | No supporting OS subroutines are required. |
---|
42 | */ |
---|
43 | |
---|
44 | /* |
---|
45 | * Copyright (c) 1990 Regents of the University of California. |
---|
46 | * All rights reserved. |
---|
47 | * |
---|
48 | * This code is derived from software contributed to Berkeley by |
---|
49 | * Chris Torek. |
---|
50 | * |
---|
51 | * Redistribution and use in source and binary forms, with or without |
---|
52 | * modification, are permitted provided that the following conditions |
---|
53 | * are met: |
---|
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. All advertising materials mentioning features or use of this software |
---|
60 | * must display the following acknowledgement: |
---|
61 | * This product includes software developed by the University of |
---|
62 | * California, Berkeley and its contributors. |
---|
63 | * 4. Neither the name of the University nor the names of its contributors |
---|
64 | * may be used to endorse or promote products derived from this software |
---|
65 | * without specific prior written permission. |
---|
66 | * |
---|
67 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
68 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
69 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
70 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
71 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
72 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
73 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
74 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
75 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
76 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
77 | * SUCH DAMAGE. |
---|
78 | */ |
---|
79 | |
---|
80 | #include <_ansi.h> |
---|
81 | #include <stdlib.h> /* div_t */ |
---|
82 | |
---|
83 | div_t |
---|
84 | div (int num, |
---|
85 | int denom) |
---|
86 | { |
---|
87 | div_t r; |
---|
88 | |
---|
89 | r.quot = num / denom; |
---|
90 | r.rem = num % denom; |
---|
91 | /* |
---|
92 | * The ANSI standard says that |r.quot| <= |n/d|, where |
---|
93 | * n/d is to be computed in infinite precision. In other |
---|
94 | * words, we should always truncate the quotient towards |
---|
95 | * 0, never -infinity or +infinity. |
---|
96 | * |
---|
97 | * Machine division and remainer may work either way when |
---|
98 | * one or both of n or d is negative. If only one is |
---|
99 | * negative and r.quot has been truncated towards -inf, |
---|
100 | * r.rem will have the same sign as denom and the opposite |
---|
101 | * sign of num; if both are negative and r.quot has been |
---|
102 | * truncated towards -inf, r.rem will be positive (will |
---|
103 | * have the opposite sign of num). These are considered |
---|
104 | * `wrong'. |
---|
105 | * |
---|
106 | * If both are num and denom are positive, r will always |
---|
107 | * be positive. |
---|
108 | * |
---|
109 | * This all boils down to: |
---|
110 | * if num >= 0, but r.rem < 0, we got the wrong answer. |
---|
111 | * In that case, to get the right answer, add 1 to r.quot and |
---|
112 | * subtract denom from r.rem. |
---|
113 | * if num < 0, but r.rem > 0, we also have the wrong answer. |
---|
114 | * In this case, to get the right answer, subtract 1 from r.quot and |
---|
115 | * add denom to r.rem. |
---|
116 | */ |
---|
117 | if (num >= 0 && r.rem < 0) { |
---|
118 | ++r.quot; |
---|
119 | r.rem -= denom; |
---|
120 | } |
---|
121 | else if (num < 0 && r.rem > 0) { |
---|
122 | --r.quot; |
---|
123 | r.rem += denom; |
---|
124 | } |
---|
125 | return (r); |
---|
126 | } |
---|