1 /* Test of splitting a 'long 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. */
29 #define ASSERT(expr) if (!(expr)) abort ();
32 my_ldexp (long double x, int d)
46 DECL_LONG_DOUBLE_ROUNDING
48 BEGIN_LONG_DOUBLE_ROUNDING ();
54 mantissa = frexpl (x, &exp);
55 ASSERT (mantissa != mantissa);
58 { /* Positive infinity. */
62 mantissa = frexpl (x, &exp);
63 ASSERT (mantissa == x);
66 { /* Negative infinity. */
70 mantissa = frexpl (x, &exp);
71 ASSERT (mantissa == x);
74 { /* Positive zero. */
78 mantissa = frexpl (x, &exp);
80 ASSERT (mantissa == x);
83 { /* Negative zero. */
87 mantissa = frexpl (x, &exp);
89 ASSERT (mantissa == x);
92 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
95 long double mantissa = frexpl (x, &exp);
97 ASSERT (mantissa == 0.5L);
99 for (i = 1, x = 1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
102 long double mantissa = frexpl (x, &exp);
104 ASSERT (mantissa == 0.5L);
106 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
109 long double mantissa = frexpl (x, &exp);
111 ASSERT (mantissa == 0.5L);
114 for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
117 long double mantissa = frexpl (x, &exp);
119 ASSERT (mantissa == -0.5L);
121 for (i = 1, x = -1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
124 long double mantissa = frexpl (x, &exp);
126 ASSERT (mantissa == -0.5L);
128 for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
131 long double mantissa = frexpl (x, &exp);
133 ASSERT (mantissa == -0.5L);
136 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
139 long double mantissa = frexpl (x, &exp);
141 ASSERT (mantissa == 0.505L);
143 for (i = 1, x = 1.01L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
146 long double mantissa = frexpl (x, &exp);
148 ASSERT (mantissa == 0.505L);
150 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
153 long double mantissa = frexpl (x, &exp);
155 ASSERT (mantissa >= 0.5L);
156 ASSERT (mantissa < 1.0L);
157 ASSERT (mantissa == my_ldexp (x, - exp));
160 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
163 long double mantissa = frexpl (x, &exp);
165 ASSERT (mantissa == 0.866025L);
167 for (i = 1, x = 1.73205L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
170 long double mantissa = frexpl (x, &exp);
172 ASSERT (mantissa == 0.866025L);
174 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
177 long double mantissa = frexpl (x, &exp);
178 ASSERT (exp == i || exp == i + 1);
179 ASSERT (mantissa >= 0.5L);
180 ASSERT (mantissa < 1.0L);
181 ASSERT (mantissa == my_ldexp (x, - exp));