1 /* Test of splitting a double into fraction and mantissa.
2 Copyright (C) 2007 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
46 static double zero = 0.0;
50 # define NaN() (0.0 / 0.0)
54 my_ldexp (double x, int d)
67 /* The use of 'volatile' guarantees that excess precision bits are dropped
68 when dealing with denormalized numbers. It is necessary on x86 systems
69 where double-floats are not IEEE compliant by default, to avoid that the
70 results become platform and compiler option dependent. 'volatile' is a
71 portable alternative to gcc's -ffloat-store option. */
78 mantissa = frexp (x, &exp);
79 ASSERT (isnan (mantissa));
82 { /* Positive infinity. */
86 mantissa = frexp (x, &exp);
87 ASSERT (mantissa == x);
90 { /* Negative infinity. */
94 mantissa = frexp (x, &exp);
95 ASSERT (mantissa == x);
98 { /* Positive zero. */
102 mantissa = frexp (x, &exp);
104 ASSERT (mantissa == x);
105 ASSERT (!signbit (mantissa));
108 { /* Negative zero. */
112 mantissa = frexp (x, &exp);
114 ASSERT (mantissa == x);
115 ASSERT (signbit (mantissa));
118 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
121 double mantissa = frexp (x, &exp);
123 ASSERT (mantissa == 0.5);
125 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
128 double mantissa = frexp (x, &exp);
130 ASSERT (mantissa == 0.5);
132 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
135 double mantissa = frexp (x, &exp);
137 ASSERT (mantissa == 0.5);
140 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
143 double mantissa = frexp (x, &exp);
145 ASSERT (mantissa == -0.5);
147 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
150 double mantissa = frexp (x, &exp);
152 ASSERT (mantissa == -0.5);
154 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
157 double mantissa = frexp (x, &exp);
159 ASSERT (mantissa == -0.5);
162 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
165 double mantissa = frexp (x, &exp);
167 ASSERT (mantissa == 0.505);
169 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
172 double mantissa = frexp (x, &exp);
174 ASSERT (mantissa == 0.505);
176 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
179 double mantissa = frexp (x, &exp);
181 ASSERT (mantissa >= 0.5);
182 ASSERT (mantissa < 1.0);
183 ASSERT (mantissa == my_ldexp (x, - exp));
186 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
189 double mantissa = frexp (x, &exp);
191 ASSERT (mantissa == 0.866025);
193 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
196 double mantissa = frexp (x, &exp);
198 ASSERT (mantissa == 0.866025);
200 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
203 double mantissa = frexp (x, &exp);
204 ASSERT (exp == i || exp == i + 1);
205 ASSERT (mantissa >= 0.5);
206 ASSERT (mantissa < 1.0);
207 ASSERT (mantissa == my_ldexp (x, - exp));