fmodl, remainder*: Avoid wrong results due to rounding errors.
[gnulib.git] / lib / round.c
1 /* Round toward nearest, breaking ties away from zero.
2    Copyright (C) 2007, 2010-2012 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with this program; if not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Ben Pfaff <blp@gnu.org>, 2007.
18    Based heavily on code by Bruno Haible. */
19
20 #if ! defined USE_LONG_DOUBLE
21 # include <config.h>
22 #endif
23
24 /* Specification.  */
25 #include <math.h>
26
27 #include <float.h>
28
29 #undef MIN
30
31 #ifdef USE_LONG_DOUBLE
32 # define ROUND roundl
33 # define FLOOR floorl
34 # define CEIL ceill
35 # define DOUBLE long double
36 # define MANT_DIG LDBL_MANT_DIG
37 # define MIN LDBL_MIN
38 # define L_(literal) literal##L
39 # define HAVE_FLOOR_AND_CEIL HAVE_FLOORL_AND_CEILL
40 #elif ! defined USE_FLOAT
41 # define ROUND round
42 # define FLOOR floor
43 # define CEIL ceil
44 # define DOUBLE double
45 # define MANT_DIG DBL_MANT_DIG
46 # define MIN DBL_MIN
47 # define L_(literal) literal
48 # define HAVE_FLOOR_AND_CEIL 1
49 #else /* defined USE_FLOAT */
50 # define ROUND roundf
51 # define FLOOR floorf
52 # define CEIL ceilf
53 # define DOUBLE float
54 # define MANT_DIG FLT_MANT_DIG
55 # define MIN FLT_MIN
56 # define L_(literal) literal##f
57 # define HAVE_FLOOR_AND_CEIL HAVE_FLOORF_AND_CEILF
58 #endif
59
60 /* -0.0.  See minus-zero.h.  */
61 #if defined __hpux || defined __sgi || defined __ICC
62 # define MINUS_ZERO (-MIN * MIN)
63 #else
64 # define MINUS_ZERO L_(-0.0)
65 #endif
66
67 /* If we're being included from test-round2[f].c, it already defined names for
68    our round implementations.  Otherwise, pick the preferred implementation for
69    this machine. */
70 #if !defined FLOOR_BASED_ROUND && !defined FLOOR_FREE_ROUND
71 # if HAVE_FLOOR_AND_CEIL
72 #  define FLOOR_BASED_ROUND ROUND
73 # else
74 #  define FLOOR_FREE_ROUND ROUND
75 # endif
76 #endif
77
78 #ifdef FLOOR_BASED_ROUND
79 /* An implementation of the C99 round function based on floor and ceil.  We use
80    this when floor and ceil are available, on the assumption that they are
81    faster than the open-coded versions below. */
82 DOUBLE
83 FLOOR_BASED_ROUND (DOUBLE x)
84 {
85   if (x >= L_(0.0))
86     {
87       DOUBLE y = FLOOR (x);
88       if (x - y >= L_(0.5))
89         y += L_(1.0);
90       return y;
91     }
92   else
93     {
94       DOUBLE y = CEIL (x);
95       if (y - x >= L_(0.5))
96         y -= L_(1.0);
97       return y;
98     }
99 }
100 #endif /* FLOOR_BASED_ROUND */
101
102 #ifdef FLOOR_FREE_ROUND
103 /* An implementation of the C99 round function without floor or ceil.
104    We use this when floor or ceil is missing. */
105 DOUBLE
106 FLOOR_FREE_ROUND (DOUBLE x)
107 {
108   /* 2^(MANT_DIG-1).  */
109   static const DOUBLE TWO_MANT_DIG =
110     /* Assume MANT_DIG <= 5 * 31.
111        Use the identity
112        n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5).  */
113     (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
114     * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
115     * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
116     * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
117     * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
118
119   /* The use of 'volatile' guarantees that excess precision bits are dropped at
120      each addition step and before the following comparison at the caller's
121      site.  It is necessary on x86 systems where double-floats are not IEEE
122      compliant by default, to avoid that the results become platform and
123      compiler option dependent.  'volatile' is a portable alternative to gcc's
124      -ffloat-store option.  */
125   volatile DOUBLE y = x;
126   volatile DOUBLE z = y;
127
128   if (z > L_(0.0))
129     {
130       /* Avoid rounding error for x = 0.5 - 2^(-MANT_DIG-1).  */
131       if (z < L_(0.5))
132         z = L_(0.0);
133       /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1.  */
134       else if (z < TWO_MANT_DIG)
135         {
136           /* Add 0.5 to the absolute value.  */
137           y = z += L_(0.5);
138           /* Round to the next integer (nearest or up or down, doesn't
139              matter).  */
140           z += TWO_MANT_DIG;
141           z -= TWO_MANT_DIG;
142           /* Enforce rounding down.  */
143           if (z > y)
144             z -= L_(1.0);
145         }
146     }
147   else if (z < L_(0.0))
148     {
149       /* Avoid rounding error for x = -(0.5 - 2^(-MANT_DIG-1)).  */
150       if (z > - L_(0.5))
151         z = MINUS_ZERO;
152       /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1.  */
153       else if (z > -TWO_MANT_DIG)
154         {
155           /* Add 0.5 to the absolute value.  */
156           y = z -= L_(0.5);
157           /* Round to the next integer (nearest or up or down, doesn't
158              matter).  */
159           z -= TWO_MANT_DIG;
160           z += TWO_MANT_DIG;
161           /* Enforce rounding up.  */
162           if (z < y)
163             z += L_(1.0);
164         }
165     }
166   return z;
167 }
168 #endif /* FLOOR_FREE_ROUND */