2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
8 /*****************************************************************************
20 Compute quotient en remainder of two int variables
24 denom = the denominator
27 a struct with two ints quot and rem with
28 quot = numer / denom and rem = numer % denom.
30 typedef struct div_t {
46 ******************************************************************************/
50 ret
.quot
= numer
/ denom
;
51 ret
.rem
= numer
% denom
;
54 This comment is from FreeBSD's src/lib/libc/stdlib/div.c, but
55 the code was written before the comment was added (see CVS log).
57 Thus the code isn't really under the BSD license. The comment is...
61 * The ANSI standard says that |r.quot| <= |n/d|, where
62 * n/d is to be computed in infinite precision. In other
63 * words, we should always truncate the quotient towards
66 * Machine division and remainer may work either way when
67 * one or both of n or d is negative. If only one is
68 * negative and r.quot has been truncated towards -inf,
69 * r.rem will have the same sign as denom and the opposite
70 * sign of num; if both are negative and r.quot has been
71 * truncated towards -inf, r.rem will be positive (will
72 * have the opposite sign of num). These are considered
75 * If both are num and denom are positive, r will always
78 * This all boils down to:
79 * if num >= 0, but r.rem < 0, we got the wrong answer.
80 * In that case, to get the right answer, add 1 to r.quot and
81 * subtract denom from r.rem.
83 if (numer
>= 0 && ret
.rem
< 0)