1 /* Test of splitting a double into fraction and mantissa.
2 Copyright (C) 2007-2009 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. */
23 #include "signature.h"
24 SIGNATURE_CHECK (frexp, double, (double, int *));
30 #include "isnand-nolibm.h"
33 /* Avoid some warnings from "gcc -Wshadow".
34 This file doesn't use the exp() function. */
38 #define ASSERT(expr) \
43 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
50 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
51 So we use -zero instead. */
55 my_ldexp (double x, int d)
68 /* The use of 'volatile' guarantees that excess precision bits are dropped
69 when dealing with denormalized numbers. It is necessary on x86 systems
70 where double-floats are not IEEE compliant by default, to avoid that the
71 results become platform and compiler option dependent. 'volatile' is a
72 portable alternative to gcc's -ffloat-store option. */
79 mantissa = frexp (x, &exp);
80 ASSERT (isnand (mantissa));
83 { /* Positive infinity. */
87 mantissa = frexp (x, &exp);
88 ASSERT (mantissa == x);
91 { /* Negative infinity. */
95 mantissa = frexp (x, &exp);
96 ASSERT (mantissa == x);
99 { /* Positive zero. */
103 mantissa = frexp (x, &exp);
105 ASSERT (mantissa == x);
106 ASSERT (!signbit (mantissa));
109 { /* Negative zero. */
113 mantissa = frexp (x, &exp);
115 ASSERT (mantissa == x);
116 ASSERT (signbit (mantissa));
119 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
122 double mantissa = frexp (x, &exp);
124 ASSERT (mantissa == 0.5);
126 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
129 double mantissa = frexp (x, &exp);
131 ASSERT (mantissa == 0.5);
133 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
136 double mantissa = frexp (x, &exp);
138 ASSERT (mantissa == 0.5);
141 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
144 double mantissa = frexp (x, &exp);
146 ASSERT (mantissa == -0.5);
148 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
151 double mantissa = frexp (x, &exp);
153 ASSERT (mantissa == -0.5);
155 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
158 double mantissa = frexp (x, &exp);
160 ASSERT (mantissa == -0.5);
163 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
166 double mantissa = frexp (x, &exp);
168 ASSERT (mantissa == 0.505);
170 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
173 double mantissa = frexp (x, &exp);
175 ASSERT (mantissa == 0.505);
177 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
180 double mantissa = frexp (x, &exp);
182 ASSERT (mantissa >= 0.5);
183 ASSERT (mantissa < 1.0);
184 ASSERT (mantissa == my_ldexp (x, - exp));
187 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
190 double mantissa = frexp (x, &exp);
192 ASSERT (mantissa == 0.866025);
194 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
197 double mantissa = frexp (x, &exp);
199 ASSERT (mantissa == 0.866025);
201 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
204 double mantissa = frexp (x, &exp);
205 ASSERT (exp == i || exp == i + 1);
206 ASSERT (mantissa >= 0.5);
207 ASSERT (mantissa < 1.0);
208 ASSERT (mantissa == my_ldexp (x, - exp));