xref: /haiku/src/system/libroot/posix/stdlib/div.c (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 /*
2  * Copyright 2002-2010, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Author:
6  * 		Daniel Reinhold, danielre@users.sf.net
7  * 		Tri-Edge AI, triedgeai@gmail.com
8  */
9 
10 
11 #include <stdlib.h>
12 
13 
14 div_t
15 div(int numerator, int denominator)
16 {
17 	div_t val;
18 
19 	val.quot = numerator / denominator;
20 	val.rem  = numerator % denominator;
21 
22 	if (numerator >= 0 && val.rem < 0) {
23 		val.rem -= denominator;
24 		++val.quot;
25 	}
26 
27 	return val;
28 }
29 
30 
31 ldiv_t
32 ldiv(long numerator, long denominator)
33 {
34 	ldiv_t val;
35 
36 	val.quot = numerator / denominator;
37 	val.rem  = numerator % denominator;
38 
39 	if (numerator >= 0 && val.rem < 0) {
40 		val.rem -= denominator;
41 		++val.quot;
42 	}
43 
44 	return val;
45 }
46 
47 
48 lldiv_t
49 lldiv(long long numerator, long long denominator)
50 {
51 	lldiv_t val;
52 
53 	val.quot = numerator / denominator;
54 	val.rem  = numerator % denominator;
55 
56 	if (numerator >= 0 && val.rem < 0) {
57 		val.rem -= denominator;
58 		++val.quot;
59 	}
60 
61 	return val;
62 }
63