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"
34 /* Avoid some warnings from "gcc -Wshadow".
35 This file doesn't use the exp() function. */
40 my_ldexp (double x, int d)
53 /* The use of 'volatile' guarantees that excess precision bits are dropped
54 when dealing with denormalized numbers. It is necessary on x86 systems
55 where double-floats are not IEEE compliant by default, to avoid that the
56 results become platform and compiler option dependent. 'volatile' is a
57 portable alternative to gcc's -ffloat-store option. */
64 mantissa = frexp (x, &exp);
65 ASSERT (isnand (mantissa));
68 { /* Positive infinity. */
72 mantissa = frexp (x, &exp);
73 ASSERT (mantissa == x);
76 { /* Negative infinity. */
80 mantissa = frexp (x, &exp);
81 ASSERT (mantissa == x);
84 { /* Positive zero. */
88 mantissa = frexp (x, &exp);
90 ASSERT (mantissa == x);
91 ASSERT (!signbit (mantissa));
94 { /* Negative zero. */
98 mantissa = frexp (x, &exp);
100 ASSERT (mantissa == x);
101 ASSERT (signbit (mantissa));
104 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
107 double mantissa = frexp (x, &exp);
109 ASSERT (mantissa == 0.5);
111 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
114 double mantissa = frexp (x, &exp);
116 ASSERT (mantissa == 0.5);
118 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
121 double mantissa = frexp (x, &exp);
123 ASSERT (mantissa == 0.5);
126 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
129 double mantissa = frexp (x, &exp);
131 ASSERT (mantissa == -0.5);
133 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
136 double mantissa = frexp (x, &exp);
138 ASSERT (mantissa == -0.5);
140 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
143 double mantissa = frexp (x, &exp);
145 ASSERT (mantissa == -0.5);
148 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
151 double mantissa = frexp (x, &exp);
153 ASSERT (mantissa == 0.505);
155 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
158 double mantissa = frexp (x, &exp);
160 ASSERT (mantissa == 0.505);
162 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
165 double mantissa = frexp (x, &exp);
167 ASSERT (mantissa >= 0.5);
168 ASSERT (mantissa < 1.0);
169 ASSERT (mantissa == my_ldexp (x, - exp));
172 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
175 double mantissa = frexp (x, &exp);
177 ASSERT (mantissa == 0.866025);
179 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
182 double mantissa = frexp (x, &exp);
184 ASSERT (mantissa == 0.866025);
186 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
189 double mantissa = frexp (x, &exp);
190 ASSERT (exp == i || exp == i + 1);
191 ASSERT (mantissa >= 0.5);
192 ASSERT (mantissa < 1.0);
193 ASSERT (mantissa == my_ldexp (x, - exp));