1 |
207 |
jeremybenn |
/*
|
2 |
|
|
FUNCTION
|
3 |
|
|
<<lldiv>>---divide two long long integers
|
4 |
|
|
|
5 |
|
|
INDEX
|
6 |
|
|
lldiv
|
7 |
|
|
|
8 |
|
|
ANSI_SYNOPSIS
|
9 |
|
|
#include <stdlib.h>
|
10 |
|
|
lldiv_t lldiv(long long <[n]>, long long <[d]>);
|
11 |
|
|
|
12 |
|
|
TRAD_SYNOPSIS
|
13 |
|
|
#include <stdlib.h>
|
14 |
|
|
lldiv_t lldiv(<[n]>, <[d]>)
|
15 |
|
|
long long <[n]>, <[d]>;
|
16 |
|
|
|
17 |
|
|
DESCRIPTION
|
18 |
|
|
Divide
|
19 |
|
|
@tex
|
20 |
|
|
$n/d$,
|
21 |
|
|
@end tex
|
22 |
|
|
@ifnottex
|
23 |
|
|
<[n]>/<[d]>,
|
24 |
|
|
@end ifnottex
|
25 |
|
|
returning quotient and remainder as two long long integers in a structure
|
26 |
|
|
<<lldiv_t>>.
|
27 |
|
|
|
28 |
|
|
RETURNS
|
29 |
|
|
The result is represented with the structure
|
30 |
|
|
|
31 |
|
|
. typedef struct
|
32 |
|
|
. {
|
33 |
|
|
. long long quot;
|
34 |
|
|
. long long rem;
|
35 |
|
|
. } lldiv_t;
|
36 |
|
|
|
37 |
|
|
where the <<quot>> field represents the quotient, and <<rem>> the
|
38 |
|
|
remainder. For nonzero <[d]>, if `<<<[r]> = ldiv(<[n]>,<[d]>);>>' then
|
39 |
|
|
<[n]> equals `<<<[r]>.rem + <[d]>*<[r]>.quot>>'.
|
40 |
|
|
|
41 |
|
|
To divide <<long>> rather than <<long long>> values, use the similar
|
42 |
|
|
function <<ldiv>>.
|
43 |
|
|
|
44 |
|
|
PORTABILITY
|
45 |
|
|
<<lldiv>> is ISO 9899 (C99) compatable.
|
46 |
|
|
|
47 |
|
|
No supporting OS subroutines are required.
|
48 |
|
|
*/
|
49 |
|
|
|
50 |
|
|
/*-
|
51 |
|
|
* Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org>
|
52 |
|
|
* All rights reserved.
|
53 |
|
|
*
|
54 |
|
|
* Redistribution and use in source and binary forms, with or without
|
55 |
|
|
* modification, are permitted provided that the following conditions
|
56 |
|
|
* are met:
|
57 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
58 |
|
|
* notice, this list of conditions and the following disclaimer.
|
59 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
60 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
61 |
|
|
* documentation and/or other materials provided with the distribution.
|
62 |
|
|
*
|
63 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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
|
73 |
|
|
* SUCH DAMAGE.
|
74 |
|
|
*/
|
75 |
|
|
|
76 |
|
|
#include <stdlib.h>
|
77 |
|
|
|
78 |
|
|
/*
|
79 |
|
|
* The ANSI standard says that |r.quot| <= |n/d|, where
|
80 |
|
|
* n/d is to be computed in infinite precision. In other
|
81 |
|
|
* words, we should always truncate the quotient towards
|
82 |
|
|
* 0, never -infinity.
|
83 |
|
|
*
|
84 |
|
|
* Machine division and remainer may work either way when
|
85 |
|
|
* one or both of n or d is negative. If only one is
|
86 |
|
|
* negative and r.quot has been truncated towards -inf,
|
87 |
|
|
* r.rem will have the same sign as denom and the opposite
|
88 |
|
|
* sign of num; if both are negative and r.quot has been
|
89 |
|
|
* truncated towards -inf, r.rem will be positive (will
|
90 |
|
|
* have the opposite sign of num). These are considered
|
91 |
|
|
* `wrong'.
|
92 |
|
|
*
|
93 |
|
|
* If both are num and denom are positive, r will always
|
94 |
|
|
* be positive.
|
95 |
|
|
*
|
96 |
|
|
* This all boils down to:
|
97 |
|
|
* if num >= 0, but r.rem < 0, we got the wrong answer.
|
98 |
|
|
* In that case, to get the right answer, add 1 to r.quot and
|
99 |
|
|
* subtract denom from r.rem.
|
100 |
|
|
*/
|
101 |
|
|
lldiv_t
|
102 |
|
|
_DEFUN (lldiv, (number, denom),
|
103 |
|
|
long long numer _AND long long denom)
|
104 |
|
|
{
|
105 |
|
|
lldiv_t retval;
|
106 |
|
|
|
107 |
|
|
retval.quot = numer / denom;
|
108 |
|
|
retval.rem = numer % denom;
|
109 |
|
|
if (numer >= 0 && retval.rem < 0) {
|
110 |
|
|
retval.quot++;
|
111 |
|
|
retval.rem -= denom;
|
112 |
|
|
}
|
113 |
|
|
return (retval);
|
114 |
|
|
}
|
115 |
|
|
|