More linking issues fixed.
[glibc.git] / stdlib / lldiv.c
blob28a016b74448ebb824a3140900de3d66501ccbd3
1 /* `long long int' divison with remainder.
2 Copyright (C) 1992, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <stdlib.h>
23 /* Return the `lldiv_t' representation of NUMER over DENOM. */
24 lldiv_t
25 lldiv (numer, denom)
26 long long int numer;
27 long long int denom;
29 lldiv_t result;
31 result.quot = numer / denom;
32 result.rem = numer % denom;
34 /* The ANSI standard says that |QUOT| <= |NUMER / DENOM|, where
35 NUMER / DENOM is to be computed in infinite precision. In
36 other words, we should always truncate the quotient towards
37 zero, never -infinity. Machine division and remainer may
38 work either way when one or both of NUMER or DENOM is
39 negative. If only one is negative and QUOT has been
40 truncated towards -infinity, REM will have the same sign as
41 DENOM and the opposite sign of NUMER; if both are negative
42 and QUOT has been truncated towards -infinity, REM will be
43 positive (will have the opposite sign of NUMER). These are
44 considered `wrong'. If both are NUM and DENOM are positive,
45 RESULT will always be positive. This all boils down to: if
46 NUMER >= 0, but REM < 0, we got the wrong answer. In that
47 case, to get the right answer, add 1 to QUOT and subtract
48 DENOM from REM. */
50 if (numer >= 0 && result.rem < 0)
52 ++result.quot;
53 result.rem -= denom;
56 return result;