xref: /haiku/src/system/libroot/posix/stdlib/div.c (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
1 /*
2  *  Copyright (c) 2002, OpenBeOS Project.
3  *  All rights reserved.
4  *  Distributed under the terms of the OpenBeOS license.
5  *
6  *
7  *  div.c:
8  *  implements the standard C library functions:
9  *    div, ldiv
10  *
11  *
12  *  Author(s):
13  *  Daniel Reinhold (danielre@users.sf.net)
14  *
15  */
16 
17 #include <stdlib.h>
18 
19 
20 div_t
21 div(int numerator, int denominator)
22 {
23 	div_t val;
24 
25 	val.quot = numerator / denominator;
26 	val.rem  = numerator - denominator * val.quot;
27 
28 	if ((val.rem > 0) && (val.quot < 0)) {
29 		val.rem -= denominator;
30 		++val.quot;
31 	}
32 
33 	return val;
34 }
35 
36 
37 ldiv_t
38 ldiv(long numerator, long denominator)
39 {
40 	ldiv_t val;
41 
42 	val.quot = numerator / denominator;
43 	val.rem  = numerator - denominator * val.quot;
44 
45 	if ((val.rem > 0) && (val.quot < 0)) {
46 		val.rem -= denominator;
47 		++val.quot;
48 	}
49 
50 	return val;
51 }
52 
53