Commit | Line | Data |
---|---|---|
b374846a KB |
1 | /* |
2 | * Copyright (c) 1990 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Chris Torek. | |
7 | * | |
8 | * %sccs.include.redist.c% | |
9 | */ | |
10 | ||
11 | #if defined(LIBC_SCCS) && !defined(lint) | |
12 | static char sccsid[] = "@(#)div.c 5.1 (Berkeley) %G%"; | |
13 | #endif /* LIBC_SCCS and not lint */ | |
14 | ||
15 | #include <stdlib.h> /* div_t */ | |
16 | ||
17 | /* | |
18 | * I AM NOT SURE THIS IS COMPLETELY PORTABLE | |
19 | * (or that it is even right) | |
20 | */ | |
21 | div_t | |
22 | div(num, denom) | |
23 | int num, denom; | |
24 | { | |
25 | div_t r; | |
26 | ||
27 | /* avoid deep thought */ | |
28 | if (num > 0 && denom < 0) { | |
29 | num = -num; | |
30 | denom = -denom; | |
31 | } | |
32 | r.quot = num / denom; | |
33 | r.rem = num % denom; | |
34 | if (num < 0 && denom > 0) { | |
35 | /* | |
36 | * Machine division and remainer may work either way. The | |
37 | * ANSI standard says that |r.quot| < |n/d| (where n/d | |
38 | * computed in infinite precision). If the remainder is | |
39 | * positive, we got the `wrong' answer, so fix it. | |
40 | */ | |
41 | if (r.rem > 0) { | |
42 | r.quot++; | |
43 | r.rem -= denom; | |
44 | } | |
45 | } | |
46 | return (r); | |
47 | } |