1 /* Test of splitting a double into fraction and mantissa.
2 Copyright (C) 2007-2010 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"
32 /* Avoid some warnings from "gcc -Wshadow".
33 This file doesn't use the exp() function. */
37 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
38 So we use -zero instead. */
42 my_ldexp (double x, int d)
55 /* The use of 'volatile' guarantees that excess precision bits are dropped
56 when dealing with denormalized numbers. It is necessary on x86 systems
57 where double-floats are not IEEE compliant by default, to avoid that the
58 results become platform and compiler option dependent. 'volatile' is a
59 portable alternative to gcc's -ffloat-store option. */
66 mantissa = frexp (x, &exp);
67 ASSERT (isnand (mantissa));
70 { /* Positive infinity. */
74 mantissa = frexp (x, &exp);
75 ASSERT (mantissa == x);
78 { /* Negative infinity. */
82 mantissa = frexp (x, &exp);
83 ASSERT (mantissa == x);
86 { /* Positive zero. */
90 mantissa = frexp (x, &exp);
92 ASSERT (mantissa == x);
93 ASSERT (!signbit (mantissa));
96 { /* Negative zero. */
100 mantissa = frexp (x, &exp);
102 ASSERT (mantissa == x);
103 ASSERT (signbit (mantissa));
106 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
109 double mantissa = frexp (x, &exp);
111 ASSERT (mantissa == 0.5);
113 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
116 double mantissa = frexp (x, &exp);
118 ASSERT (mantissa == 0.5);
120 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
123 double mantissa = frexp (x, &exp);
125 ASSERT (mantissa == 0.5);
128 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
131 double mantissa = frexp (x, &exp);
133 ASSERT (mantissa == -0.5);
135 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
138 double mantissa = frexp (x, &exp);
140 ASSERT (mantissa == -0.5);
142 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
145 double mantissa = frexp (x, &exp);
147 ASSERT (mantissa == -0.5);
150 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
153 double mantissa = frexp (x, &exp);
155 ASSERT (mantissa == 0.505);
157 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
160 double mantissa = frexp (x, &exp);
162 ASSERT (mantissa == 0.505);
164 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
167 double mantissa = frexp (x, &exp);
169 ASSERT (mantissa >= 0.5);
170 ASSERT (mantissa < 1.0);
171 ASSERT (mantissa == my_ldexp (x, - exp));
174 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
177 double mantissa = frexp (x, &exp);
179 ASSERT (mantissa == 0.866025);
181 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
184 double mantissa = frexp (x, &exp);
186 ASSERT (mantissa == 0.866025);
188 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
191 double mantissa = frexp (x, &exp);
192 ASSERT (exp == i || exp == i + 1);
193 ASSERT (mantissa >= 0.5);
194 ASSERT (mantissa < 1.0);
195 ASSERT (mantissa == my_ldexp (x, - exp));