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__); \
47 my_ldexp (double x, int d)
60 /* The use of 'volatile' guarantees that excess precision bits are dropped
61 when dealing with denormalized numbers. It is necessary on x86 systems
62 where double-floats are not IEEE compliant by default, to avoid that the
63 results become platform and compiler option dependent. 'volatile' is a
64 portable alternative to gcc's -ffloat-store option. */
71 mantissa = frexp (x, &exp);
72 ASSERT (isnand (mantissa));
75 { /* Positive infinity. */
79 mantissa = frexp (x, &exp);
80 ASSERT (mantissa == x);
83 { /* Negative infinity. */
87 mantissa = frexp (x, &exp);
88 ASSERT (mantissa == x);
91 { /* Positive zero. */
95 mantissa = frexp (x, &exp);
97 ASSERT (mantissa == x);
98 ASSERT (!signbit (mantissa));
101 { /* Negative zero. */
105 mantissa = frexp (x, &exp);
107 ASSERT (mantissa == x);
108 ASSERT (signbit (mantissa));
111 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
114 double mantissa = frexp (x, &exp);
116 ASSERT (mantissa == 0.5);
118 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
121 double mantissa = frexp (x, &exp);
123 ASSERT (mantissa == 0.5);
125 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
128 double mantissa = frexp (x, &exp);
130 ASSERT (mantissa == 0.5);
133 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
136 double mantissa = frexp (x, &exp);
138 ASSERT (mantissa == -0.5);
140 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
143 double mantissa = frexp (x, &exp);
145 ASSERT (mantissa == -0.5);
147 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
150 double mantissa = frexp (x, &exp);
152 ASSERT (mantissa == -0.5);
155 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
158 double mantissa = frexp (x, &exp);
160 ASSERT (mantissa == 0.505);
162 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
165 double mantissa = frexp (x, &exp);
167 ASSERT (mantissa == 0.505);
169 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
172 double mantissa = frexp (x, &exp);
174 ASSERT (mantissa >= 0.5);
175 ASSERT (mantissa < 1.0);
176 ASSERT (mantissa == my_ldexp (x, - exp));
179 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
182 double mantissa = frexp (x, &exp);
184 ASSERT (mantissa == 0.866025);
186 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
189 double mantissa = frexp (x, &exp);
191 ASSERT (mantissa == 0.866025);
193 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
196 double mantissa = frexp (x, &exp);
197 ASSERT (exp == i || exp == i + 1);
198 ASSERT (mantissa >= 0.5);
199 ASSERT (mantissa < 1.0);
200 ASSERT (mantissa == my_ldexp (x, - exp));