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. */
29 #define ASSERT(expr) \
34 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41 my_ldexp (double x, int d)
54 /* The use of 'volatile' guarantees that excess precision bits are dropped
55 when dealing with denormalized numbers. It is necessary on x86 systems
56 where double-floats are not IEEE compliant by default, to avoid that the
57 results become platform and compiler option dependent. 'volatile' is a
58 portable alternative to gcc's -ffloat-store option. */
65 mantissa = frexp (x, &exp);
66 ASSERT (isnand (mantissa));
69 { /* Positive infinity. */
73 mantissa = frexp (x, &exp);
74 ASSERT (mantissa == x);
77 { /* Negative infinity. */
81 mantissa = frexp (x, &exp);
82 ASSERT (mantissa == x);
85 { /* Positive zero. */
89 mantissa = frexp (x, &exp);
91 ASSERT (mantissa == x);
92 ASSERT (!signbit (mantissa));
95 { /* Negative zero. */
99 mantissa = frexp (x, &exp);
101 ASSERT (mantissa == x);
102 ASSERT (signbit (mantissa));
105 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
108 double mantissa = frexp (x, &exp);
110 ASSERT (mantissa == 0.5);
112 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
115 double mantissa = frexp (x, &exp);
117 ASSERT (mantissa == 0.5);
119 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
122 double mantissa = frexp (x, &exp);
124 ASSERT (mantissa == 0.5);
127 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
130 double mantissa = frexp (x, &exp);
132 ASSERT (mantissa == -0.5);
134 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
137 double mantissa = frexp (x, &exp);
139 ASSERT (mantissa == -0.5);
141 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
144 double mantissa = frexp (x, &exp);
146 ASSERT (mantissa == -0.5);
149 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
152 double mantissa = frexp (x, &exp);
154 ASSERT (mantissa == 0.505);
156 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
159 double mantissa = frexp (x, &exp);
161 ASSERT (mantissa == 0.505);
163 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
166 double mantissa = frexp (x, &exp);
168 ASSERT (mantissa >= 0.5);
169 ASSERT (mantissa < 1.0);
170 ASSERT (mantissa == my_ldexp (x, - exp));
173 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
176 double mantissa = frexp (x, &exp);
178 ASSERT (mantissa == 0.866025);
180 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
183 double mantissa = frexp (x, &exp);
185 ASSERT (mantissa == 0.866025);
187 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
190 double mantissa = frexp (x, &exp);
191 ASSERT (exp == i || exp == i + 1);
192 ASSERT (mantissa >= 0.5);
193 ASSERT (mantissa < 1.0);
194 ASSERT (mantissa == my_ldexp (x, - exp));