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