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);
93 { /* Negative zero. */
97 mantissa = frexpl (x, &exp);
99 ASSERT (mantissa == x);
102 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
105 long double mantissa = frexpl (x, &exp);
107 ASSERT (mantissa == 0.5L);
109 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
112 long double mantissa = frexpl (x, &exp);
114 ASSERT (mantissa == 0.5L);
116 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
119 long double mantissa = frexpl (x, &exp);
121 ASSERT (mantissa == 0.5L);
124 for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
127 long double mantissa = frexpl (x, &exp);
129 ASSERT (mantissa == -0.5L);
131 for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
134 long double mantissa = frexpl (x, &exp);
136 ASSERT (mantissa == -0.5L);
138 for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
141 long double mantissa = frexpl (x, &exp);
143 ASSERT (mantissa == -0.5L);
146 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
149 long double mantissa = frexpl (x, &exp);
151 ASSERT (mantissa == 0.505L);
153 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
156 long double mantissa = frexpl (x, &exp);
158 ASSERT (mantissa == 0.505L);
160 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
163 long double mantissa = frexpl (x, &exp);
165 ASSERT (mantissa >= 0.5L);
166 ASSERT (mantissa < 1.0L);
167 ASSERT (mantissa == my_ldexp (x, - exp));
170 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
173 long double mantissa = frexpl (x, &exp);
175 ASSERT (mantissa == 0.866025L);
177 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
180 long double mantissa = frexpl (x, &exp);
182 ASSERT (mantissa == 0.866025L);
184 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
187 long double mantissa = frexpl (x, &exp);
188 ASSERT (exp == i || exp == i + 1);
189 ASSERT (mantissa >= 0.5L);
190 ASSERT (mantissa < 1.0L);
191 ASSERT (mantissa == my_ldexp (x, - exp));