1 /* Test of splitting a double into fraction and mantissa.
2 Copyright (C) 2007-2011 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 *));
28 #include "isnand-nolibm.h"
29 #include "minus-zero.h"
33 /* Avoid some warnings from "gcc -Wshadow".
34 This file doesn't use the exp() function. */
39 my_ldexp (double x, int d)
52 /* The use of 'volatile' guarantees that excess precision bits are dropped
53 when dealing with denormalized numbers. It is necessary on x86 systems
54 where double-floats are not IEEE compliant by default, to avoid that the
55 results become platform and compiler option dependent. 'volatile' is a
56 portable alternative to gcc's -ffloat-store option. */
63 mantissa = frexp (x, &exp);
64 ASSERT (isnand (mantissa));
67 { /* Positive infinity. */
71 mantissa = frexp (x, &exp);
72 ASSERT (mantissa == x);
75 { /* Negative infinity. */
79 mantissa = frexp (x, &exp);
80 ASSERT (mantissa == x);
83 { /* Positive zero. */
87 mantissa = frexp (x, &exp);
89 ASSERT (mantissa == x);
90 ASSERT (!signbit (mantissa));
93 { /* Negative zero. */
97 mantissa = frexp (x, &exp);
99 ASSERT (mantissa == x);
100 ASSERT (signbit (mantissa));
103 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
106 double mantissa = frexp (x, &exp);
108 ASSERT (mantissa == 0.5);
110 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
113 double mantissa = frexp (x, &exp);
115 ASSERT (mantissa == 0.5);
117 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
120 double mantissa = frexp (x, &exp);
122 ASSERT (mantissa == 0.5);
125 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
128 double mantissa = frexp (x, &exp);
130 ASSERT (mantissa == -0.5);
132 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
135 double mantissa = frexp (x, &exp);
137 ASSERT (mantissa == -0.5);
139 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
142 double mantissa = frexp (x, &exp);
144 ASSERT (mantissa == -0.5);
147 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
150 double mantissa = frexp (x, &exp);
152 ASSERT (mantissa == 0.505);
154 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
157 double mantissa = frexp (x, &exp);
159 ASSERT (mantissa == 0.505);
161 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
164 double mantissa = frexp (x, &exp);
166 ASSERT (mantissa >= 0.5);
167 ASSERT (mantissa < 1.0);
168 ASSERT (mantissa == my_ldexp (x, - exp));
171 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
174 double mantissa = frexp (x, &exp);
176 ASSERT (mantissa == 0.866025);
178 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
181 double mantissa = frexp (x, &exp);
183 ASSERT (mantissa == 0.866025);
185 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
188 double mantissa = frexp (x, &exp);
189 ASSERT (exp == i || exp == i + 1);
190 ASSERT (mantissa >= 0.5);
191 ASSERT (mantissa < 1.0);
192 ASSERT (mantissa == my_ldexp (x, - exp));