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. */
27 #include "isnand-nolibm.h"
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__); \
47 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
48 So we use -zero instead. */
52 my_ldexp (double x, int d)
65 /* The use of 'volatile' guarantees that excess precision bits are dropped
66 when dealing with denormalized numbers. It is necessary on x86 systems
67 where double-floats are not IEEE compliant by default, to avoid that the
68 results become platform and compiler option dependent. 'volatile' is a
69 portable alternative to gcc's -ffloat-store option. */
76 mantissa = frexp (x, &exp);
77 ASSERT (isnand (mantissa));
80 { /* Positive infinity. */
84 mantissa = frexp (x, &exp);
85 ASSERT (mantissa == x);
88 { /* Negative infinity. */
92 mantissa = frexp (x, &exp);
93 ASSERT (mantissa == x);
96 { /* Positive zero. */
100 mantissa = frexp (x, &exp);
102 ASSERT (mantissa == x);
103 ASSERT (!signbit (mantissa));
106 { /* Negative zero. */
110 mantissa = frexp (x, &exp);
112 ASSERT (mantissa == x);
113 ASSERT (signbit (mantissa));
116 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
119 double mantissa = frexp (x, &exp);
121 ASSERT (mantissa == 0.5);
123 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
126 double mantissa = frexp (x, &exp);
128 ASSERT (mantissa == 0.5);
130 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
133 double mantissa = frexp (x, &exp);
135 ASSERT (mantissa == 0.5);
138 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
141 double mantissa = frexp (x, &exp);
143 ASSERT (mantissa == -0.5);
145 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
148 double mantissa = frexp (x, &exp);
150 ASSERT (mantissa == -0.5);
152 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
155 double mantissa = frexp (x, &exp);
157 ASSERT (mantissa == -0.5);
160 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
163 double mantissa = frexp (x, &exp);
165 ASSERT (mantissa == 0.505);
167 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
170 double mantissa = frexp (x, &exp);
172 ASSERT (mantissa == 0.505);
174 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
177 double mantissa = frexp (x, &exp);
179 ASSERT (mantissa >= 0.5);
180 ASSERT (mantissa < 1.0);
181 ASSERT (mantissa == my_ldexp (x, - exp));
184 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
187 double mantissa = frexp (x, &exp);
189 ASSERT (mantissa == 0.866025);
191 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
194 double mantissa = frexp (x, &exp);
196 ASSERT (mantissa == 0.866025);
198 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
201 double mantissa = frexp (x, &exp);
202 ASSERT (exp == i || exp == i + 1);
203 ASSERT (mantissa >= 0.5);
204 ASSERT (mantissa < 1.0);
205 ASSERT (mantissa == my_ldexp (x, - exp));