1 /* Test of splitting a double into fraction and mantissa.
2 Copyright (C) 2007 Free Software Foundation, Inc.
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 3 of the License, or
7 (at your option) any later version.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
29 #define ASSERT(expr) \
34 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
40 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
45 static double zero = 0.0;
49 # define NaN() (0.0 / 0.0)
53 my_ldexp (double x, int d)
66 /* The use of 'volatile' guarantees that excess precision bits are dropped
67 when dealing with denormalized numbers. It is necessary on x86 systems
68 where double-floats are not IEEE compliant by default, to avoid that the
69 results become platform and compiler option dependent. 'volatile' is a
70 portable alternative to gcc's -ffloat-store option. */
77 mantissa = frexp (x, &exp);
78 ASSERT (isnan (mantissa));
81 { /* Positive infinity. */
85 mantissa = frexp (x, &exp);
86 ASSERT (mantissa == x);
89 { /* Negative infinity. */
93 mantissa = frexp (x, &exp);
94 ASSERT (mantissa == x);
97 { /* Positive zero. */
101 mantissa = frexp (x, &exp);
103 ASSERT (mantissa == x);
104 ASSERT (!signbit (mantissa));
107 { /* Negative zero. */
111 mantissa = frexp (x, &exp);
113 ASSERT (mantissa == x);
114 ASSERT (signbit (mantissa));
117 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
120 double mantissa = frexp (x, &exp);
122 ASSERT (mantissa == 0.5);
124 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
127 double mantissa = frexp (x, &exp);
129 ASSERT (mantissa == 0.5);
131 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
134 double mantissa = frexp (x, &exp);
136 ASSERT (mantissa == 0.5);
139 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
142 double mantissa = frexp (x, &exp);
144 ASSERT (mantissa == -0.5);
146 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
149 double mantissa = frexp (x, &exp);
151 ASSERT (mantissa == -0.5);
153 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
156 double mantissa = frexp (x, &exp);
158 ASSERT (mantissa == -0.5);
161 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
164 double mantissa = frexp (x, &exp);
166 ASSERT (mantissa == 0.505);
168 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
171 double mantissa = frexp (x, &exp);
173 ASSERT (mantissa == 0.505);
175 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
178 double mantissa = frexp (x, &exp);
180 ASSERT (mantissa >= 0.5);
181 ASSERT (mantissa < 1.0);
182 ASSERT (mantissa == my_ldexp (x, - exp));
185 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
188 double mantissa = frexp (x, &exp);
190 ASSERT (mantissa == 0.866025);
192 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
195 double mantissa = frexp (x, &exp);
197 ASSERT (mantissa == 0.866025);
199 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
202 double mantissa = frexp (x, &exp);
203 ASSERT (exp == i || exp == i + 1);
204 ASSERT (mantissa >= 0.5);
205 ASSERT (mantissa < 1.0);
206 ASSERT (mantissa == my_ldexp (x, - exp));