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);
97 { /* Negative zero. */
101 mantissa = frexp (x, &exp);
103 ASSERT (mantissa == x);
106 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
109 double mantissa = frexp (x, &exp);
111 ASSERT (mantissa == 0.5);
113 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
116 double mantissa = frexp (x, &exp);
118 ASSERT (mantissa == 0.5);
120 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
123 double mantissa = frexp (x, &exp);
125 ASSERT (mantissa == 0.5);
128 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
131 double mantissa = frexp (x, &exp);
133 ASSERT (mantissa == -0.5);
135 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
138 double mantissa = frexp (x, &exp);
140 ASSERT (mantissa == -0.5);
142 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
145 double mantissa = frexp (x, &exp);
147 ASSERT (mantissa == -0.5);
150 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
153 double mantissa = frexp (x, &exp);
155 ASSERT (mantissa == 0.505);
157 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
160 double mantissa = frexp (x, &exp);
162 ASSERT (mantissa == 0.505);
164 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
167 double mantissa = frexp (x, &exp);
169 ASSERT (mantissa >= 0.5);
170 ASSERT (mantissa < 1.0);
171 ASSERT (mantissa == my_ldexp (x, - exp));
174 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
177 double mantissa = frexp (x, &exp);
179 ASSERT (mantissa == 0.866025);
181 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
184 double mantissa = frexp (x, &exp);
186 ASSERT (mantissa == 0.866025);
188 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
191 double mantissa = frexp (x, &exp);
192 ASSERT (exp == i || exp == i + 1);
193 ASSERT (mantissa >= 0.5);
194 ASSERT (mantissa < 1.0);
195 ASSERT (mantissa == my_ldexp (x, - exp));