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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
29 #define ASSERT(expr) if (!(expr)) abort ();
31 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
36 static double zero = 0.0;
40 # define NaN() (0.0 / 0.0)
44 my_ldexp (double x, int d)
57 /* The use of 'volatile' guarantees that excess precision bits are dropped
58 when dealing with denormalized numbers. It is necessary on x86 systems
59 where double-floats are not IEEE compliant by default, to avoid that the
60 results become platform and compiler option dependent. 'volatile' is a
61 portable alternative to gcc's -ffloat-store option. */
68 mantissa = frexp (x, &exp);
69 ASSERT (isnan (mantissa));
72 { /* Positive infinity. */
76 mantissa = frexp (x, &exp);
77 ASSERT (mantissa == x);
80 { /* Negative infinity. */
84 mantissa = frexp (x, &exp);
85 ASSERT (mantissa == x);
88 { /* Positive zero. */
92 mantissa = frexp (x, &exp);
94 ASSERT (mantissa == x);
95 ASSERT (!signbit (mantissa));
98 { /* Negative zero. */
102 mantissa = frexp (x, &exp);
104 ASSERT (mantissa == x);
105 ASSERT (signbit (mantissa));
108 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
111 double mantissa = frexp (x, &exp);
113 ASSERT (mantissa == 0.5);
115 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
118 double mantissa = frexp (x, &exp);
120 ASSERT (mantissa == 0.5);
122 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
125 double mantissa = frexp (x, &exp);
127 ASSERT (mantissa == 0.5);
130 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
133 double mantissa = frexp (x, &exp);
135 ASSERT (mantissa == -0.5);
137 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
140 double mantissa = frexp (x, &exp);
142 ASSERT (mantissa == -0.5);
144 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
147 double mantissa = frexp (x, &exp);
149 ASSERT (mantissa == -0.5);
152 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
155 double mantissa = frexp (x, &exp);
157 ASSERT (mantissa == 0.505);
159 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
162 double mantissa = frexp (x, &exp);
164 ASSERT (mantissa == 0.505);
166 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
169 double mantissa = frexp (x, &exp);
171 ASSERT (mantissa >= 0.5);
172 ASSERT (mantissa < 1.0);
173 ASSERT (mantissa == my_ldexp (x, - exp));
176 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
179 double mantissa = frexp (x, &exp);
181 ASSERT (mantissa == 0.866025);
183 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
186 double mantissa = frexp (x, &exp);
188 ASSERT (mantissa == 0.866025);
190 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
193 double mantissa = frexp (x, &exp);
194 ASSERT (exp == i || exp == i + 1);
195 ASSERT (mantissa >= 0.5);
196 ASSERT (mantissa < 1.0);
197 ASSERT (mantissa == my_ldexp (x, - exp));