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. */
27 #define ASSERT(expr) if (!(expr)) abort ();
29 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */
34 static double zero = 0.0;
38 # define NaN() (0.0 / 0.0)
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 (mantissa != 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);
95 { /* Negative zero. */
99 mantissa = frexp (x, &exp);
101 ASSERT (mantissa == x);
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));