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. */
35 #define ASSERT(expr) \
40 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
48 my_ldexp (double x, int d)
61 /* The use of 'volatile' guarantees that excess precision bits are dropped
62 when dealing with denormalized numbers. It is necessary on x86 systems
63 where double-floats are not IEEE compliant by default, to avoid that the
64 results become platform and compiler option dependent. 'volatile' is a
65 portable alternative to gcc's -ffloat-store option. */
72 mantissa = frexp (x, &exp);
73 ASSERT (isnand (mantissa));
76 { /* Positive infinity. */
80 mantissa = frexp (x, &exp);
81 ASSERT (mantissa == x);
84 { /* Negative infinity. */
88 mantissa = frexp (x, &exp);
89 ASSERT (mantissa == x);
92 { /* Positive zero. */
96 mantissa = frexp (x, &exp);
98 ASSERT (mantissa == x);
99 ASSERT (!signbit (mantissa));
102 { /* Negative zero. */
106 mantissa = frexp (x, &exp);
108 ASSERT (mantissa == x);
109 ASSERT (signbit (mantissa));
112 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
115 double mantissa = frexp (x, &exp);
117 ASSERT (mantissa == 0.5);
119 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
122 double mantissa = frexp (x, &exp);
124 ASSERT (mantissa == 0.5);
126 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
129 double mantissa = frexp (x, &exp);
131 ASSERT (mantissa == 0.5);
134 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
137 double mantissa = frexp (x, &exp);
139 ASSERT (mantissa == -0.5);
141 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
144 double mantissa = frexp (x, &exp);
146 ASSERT (mantissa == -0.5);
148 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
151 double mantissa = frexp (x, &exp);
153 ASSERT (mantissa == -0.5);
156 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
159 double mantissa = frexp (x, &exp);
161 ASSERT (mantissa == 0.505);
163 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
166 double mantissa = frexp (x, &exp);
168 ASSERT (mantissa == 0.505);
170 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
173 double mantissa = frexp (x, &exp);
175 ASSERT (mantissa >= 0.5);
176 ASSERT (mantissa < 1.0);
177 ASSERT (mantissa == my_ldexp (x, - exp));
180 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
183 double mantissa = frexp (x, &exp);
185 ASSERT (mantissa == 0.866025);
187 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
190 double mantissa = frexp (x, &exp);
192 ASSERT (mantissa == 0.866025);
194 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
197 double mantissa = frexp (x, &exp);
198 ASSERT (exp == i || exp == i + 1);
199 ASSERT (mantissa >= 0.5);
200 ASSERT (mantissa < 1.0);
201 ASSERT (mantissa == my_ldexp (x, - exp));