Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4921 Serge 1
/*
2
FUNCTION
3
<>---divide two long integers
4
 
5
INDEX
6
	ldiv
7
 
8
ANSI_SYNOPSIS
9
	#include 
10
	ldiv_t ldiv(long <[n]>, long <[d]>);
11
 
12
TRAD_SYNOPSIS
13
	#include 
14
	ldiv_t ldiv(<[n]>, <[d]>)
15
	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 integers in a structure <>.
26
 
27
RETURNS
28
The result is represented with the structure
29
 
30
. typedef struct
31
. {
32
.  long quot;
33
.  long rem;
34
. } ldiv_t;
35
 
36
where the <> field represents the quotient, and <> the
37
remainder.  For nonzero <[d]>, if `<<<[r]> = ldiv(<[n]>,<[d]>);>>' then
38
<[n]> equals `<<<[r]>.rem + <[d]>*<[r]>.quot>>'.
39
 
40
To divide <> rather than <> values, use the similar
41
function <
>.
42
 
43
PORTABILITY
44
<> is ANSI.
45
 
46
No supporting OS subroutines are required.
47
*/
48
 
49
 
50
/*
51
 * Copyright (c) 1990 Regents of the University of California.
52
 * All rights reserved.
53
 *
54
 * This code is derived from software contributed to Berkeley by
55
 * Chris Torek.
56
 *
57
 * Redistribution and use in source and binary forms, with or without
58
 * modification, are permitted provided that the following conditions
59
 * are met:
60
 * 1. Redistributions of source code must retain the above copyright
61
 *    notice, this list of conditions and the following disclaimer.
62
 * 2. Redistributions in binary form must reproduce the above copyright
63
 *    notice, this list of conditions and the following disclaimer in the
64
 *    documentation and/or other materials provided with the distribution.
65
 * 3. All advertising materials mentioning features or use of this software
66
 *    must display the following acknowledgement:
67
 *	This product includes software developed by the University of
68
 *	California, Berkeley and its contributors.
69
 * 4. Neither the name of the University nor the names of its contributors
70
 *    may be used to endorse or promote products derived from this software
71
 *    without specific prior written permission.
72
 *
73
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
74
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
77
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83
 * SUCH DAMAGE.
84
 */
85
 
86
#include <_ansi.h>
87
#include 		/* ldiv_t */
88
 
89
ldiv_t
90
_DEFUN (ldiv, (num, denom),
91
        long num _AND
92
        long denom)
93
{
94
	ldiv_t r;
95
 
96
	/* see div.c for comments */
97
 
98
	r.quot = num / denom;
99
	r.rem = num % denom;
100
	if (num >= 0 && r.rem < 0) {
101
		++r.quot;
102
		r.rem -= denom;
103
	}
104
	else if (num < 0 && r.rem > 0) {
105
		--r.quot;
106
		r.rem += denom;
107
	}
108
	return (r);
109
}