1 /* Test of splitting a double into fraction and mantissa.
2 Copyright (C) 2007-2008 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. */
30 /* Avoid some warnings from "gcc -Wshadow".
31 This file doesn't use the exp() function. */
34 #define ASSERT(expr) \
39 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
46 my_ldexp (double x, int d)
59 /* The use of 'volatile' guarantees that excess precision bits are dropped
60 when dealing with denormalized numbers. It is necessary on x86 systems
61 where double-floats are not IEEE compliant by default, to avoid that the
62 results become platform and compiler option dependent. 'volatile' is a
63 portable alternative to gcc's -ffloat-store option. */
70 mantissa = frexp (x, &exp);
71 ASSERT (isnand (mantissa));
74 { /* Positive infinity. */
78 mantissa = frexp (x, &exp);
79 ASSERT (mantissa == x);
82 { /* Negative infinity. */
86 mantissa = frexp (x, &exp);
87 ASSERT (mantissa == x);
90 { /* Positive zero. */
94 mantissa = frexp (x, &exp);
96 ASSERT (mantissa == x);
97 ASSERT (!signbit (mantissa));
100 { /* Negative zero. */
104 mantissa = frexp (x, &exp);
106 ASSERT (mantissa == x);
107 ASSERT (signbit (mantissa));
110 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
113 double mantissa = frexp (x, &exp);
115 ASSERT (mantissa == 0.5);
117 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
120 double mantissa = frexp (x, &exp);
122 ASSERT (mantissa == 0.5);
124 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
127 double mantissa = frexp (x, &exp);
129 ASSERT (mantissa == 0.5);
132 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
135 double mantissa = frexp (x, &exp);
137 ASSERT (mantissa == -0.5);
139 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
142 double mantissa = frexp (x, &exp);
144 ASSERT (mantissa == -0.5);
146 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
149 double mantissa = frexp (x, &exp);
151 ASSERT (mantissa == -0.5);
154 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
157 double mantissa = frexp (x, &exp);
159 ASSERT (mantissa == 0.505);
161 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
164 double mantissa = frexp (x, &exp);
166 ASSERT (mantissa == 0.505);
168 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
171 double mantissa = frexp (x, &exp);
173 ASSERT (mantissa >= 0.5);
174 ASSERT (mantissa < 1.0);
175 ASSERT (mantissa == my_ldexp (x, - exp));
178 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
181 double mantissa = frexp (x, &exp);
183 ASSERT (mantissa == 0.866025);
185 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
188 double mantissa = frexp (x, &exp);
190 ASSERT (mantissa == 0.866025);
192 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
195 double mantissa = frexp (x, &exp);
196 ASSERT (exp == i || exp == i + 1);
197 ASSERT (mantissa >= 0.5);
198 ASSERT (mantissa < 1.0);
199 ASSERT (mantissa == my_ldexp (x, - exp));