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. */
28 #include "isnanl-nolibm.h"
30 #define ASSERT(expr) if (!(expr)) abort ();
32 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
33 exponent for 'long double' is -964. For exponents below that, the
34 precision may be truncated to the precision used for 'double'. */
36 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
38 # define MIN_NORMAL_EXP LDBL_MIN_EXP
42 my_ldexp (long double x, int d)
56 DECL_LONG_DOUBLE_ROUNDING
58 BEGIN_LONG_DOUBLE_ROUNDING ();
64 mantissa = frexpl (x, &exp);
65 ASSERT (isnanl (mantissa));
68 { /* Positive infinity. */
72 mantissa = frexpl (x, &exp);
73 ASSERT (mantissa == x);
76 { /* Negative infinity. */
80 mantissa = frexpl (x, &exp);
81 ASSERT (mantissa == x);
84 { /* Positive zero. */
88 mantissa = frexpl (x, &exp);
90 ASSERT (mantissa == x);
91 ASSERT (!signbit (mantissa));
94 { /* Negative zero. */
98 mantissa = frexpl (x, &exp);
100 ASSERT (mantissa == x);
101 ASSERT (signbit (mantissa));
104 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
107 long double mantissa = frexpl (x, &exp);
109 ASSERT (mantissa == 0.5L);
111 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
114 long double mantissa = frexpl (x, &exp);
116 ASSERT (mantissa == 0.5L);
118 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
121 long double mantissa = frexpl (x, &exp);
123 ASSERT (mantissa == 0.5L);
126 for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
129 long double mantissa = frexpl (x, &exp);
131 ASSERT (mantissa == -0.5L);
133 for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
136 long double mantissa = frexpl (x, &exp);
138 ASSERT (mantissa == -0.5L);
140 for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
143 long double mantissa = frexpl (x, &exp);
145 ASSERT (mantissa == -0.5L);
148 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
151 long double mantissa = frexpl (x, &exp);
153 ASSERT (mantissa == 0.505L);
155 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
158 long double mantissa = frexpl (x, &exp);
160 ASSERT (mantissa == 0.505L);
162 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
165 long double mantissa = frexpl (x, &exp);
167 ASSERT (mantissa >= 0.5L);
168 ASSERT (mantissa < 1.0L);
169 ASSERT (mantissa == my_ldexp (x, - exp));
172 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
175 long double mantissa = frexpl (x, &exp);
177 ASSERT (mantissa == 0.866025L);
179 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
182 long double mantissa = frexpl (x, &exp);
184 ASSERT (mantissa == 0.866025L);
186 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
189 long double mantissa = frexpl (x, &exp);
190 ASSERT (exp == i || exp == i + 1);
191 ASSERT (mantissa >= 0.5L);
192 ASSERT (mantissa < 1.0L);
193 ASSERT (mantissa == my_ldexp (x, - exp));