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 ();
30 my_ldexp (double x, int d)
43 /* The use of 'volatile' guarantees that excess precision bits are dropped
44 when dealing with denormalized numbers. It is necessary on x86 systems
45 where double-floats are not IEEE compliant by default, to avoid that the
46 results become platform and compiler option dependent. 'volatile' is a
47 portable alternative to gcc's -ffloat-store option. */
54 mantissa = frexp (x, &exp);
55 ASSERT (mantissa != mantissa);
58 { /* Positive infinity. */
62 mantissa = frexp (x, &exp);
63 ASSERT (mantissa == x);
66 { /* Negative infinity. */
70 mantissa = frexp (x, &exp);
71 ASSERT (mantissa == x);
74 { /* Positive zero. */
78 mantissa = frexp (x, &exp);
80 ASSERT (mantissa == x);
83 { /* Negative zero. */
87 mantissa = frexp (x, &exp);
89 ASSERT (mantissa == x);
92 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
95 double mantissa = frexp (x, &exp);
97 ASSERT (mantissa == 0.5);
99 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
102 double mantissa = frexp (x, &exp);
104 ASSERT (mantissa == 0.5);
106 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
109 double mantissa = frexp (x, &exp);
111 ASSERT (mantissa == 0.5);
114 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
117 double mantissa = frexp (x, &exp);
119 ASSERT (mantissa == -0.5);
121 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
124 double mantissa = frexp (x, &exp);
126 ASSERT (mantissa == -0.5);
128 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
131 double mantissa = frexp (x, &exp);
133 ASSERT (mantissa == -0.5);
136 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
139 double mantissa = frexp (x, &exp);
141 ASSERT (mantissa == 0.505);
143 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
146 double mantissa = frexp (x, &exp);
148 ASSERT (mantissa == 0.505);
150 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
153 double mantissa = frexp (x, &exp);
155 ASSERT (mantissa >= 0.5);
156 ASSERT (mantissa < 1.0);
157 ASSERT (mantissa == my_ldexp (x, - exp));
160 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
163 double mantissa = frexp (x, &exp);
165 ASSERT (mantissa == 0.866025);
167 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
170 double mantissa = frexp (x, &exp);
172 ASSERT (mantissa == 0.866025);
174 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
177 double mantissa = frexp (x, &exp);
178 ASSERT (exp == i || exp == i + 1);
179 ASSERT (mantissa >= 0.5);
180 ASSERT (mantissa < 1.0);
181 ASSERT (mantissa == my_ldexp (x, - exp));