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 #include "isnanl-nolibm.h"
31 #define ASSERT(expr) \
36 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
42 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
43 exponent for 'long double' is -964. For exponents below that, the
44 precision may be truncated to the precision used for 'double'. */
46 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
48 # define MIN_NORMAL_EXP LDBL_MIN_EXP
52 my_ldexp (long double x, int d)
66 DECL_LONG_DOUBLE_ROUNDING
68 BEGIN_LONG_DOUBLE_ROUNDING ();
74 mantissa = frexpl (x, &exp);
75 ASSERT (isnanl (mantissa));
78 { /* Positive infinity. */
82 mantissa = frexpl (x, &exp);
83 ASSERT (mantissa == x);
86 { /* Negative infinity. */
90 mantissa = frexpl (x, &exp);
91 ASSERT (mantissa == x);
94 { /* Positive zero. */
98 mantissa = frexpl (x, &exp);
100 ASSERT (mantissa == x);
101 ASSERT (!signbit (mantissa));
104 { /* Negative zero. */
106 long double mantissa;
108 mantissa = frexpl (x, &exp);
110 ASSERT (mantissa == x);
111 ASSERT (signbit (mantissa));
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 >= MIN_NORMAL_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.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
139 long double mantissa = frexpl (x, &exp);
141 ASSERT (mantissa == -0.5L);
143 for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
146 long double mantissa = frexpl (x, &exp);
148 ASSERT (mantissa == -0.5L);
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);
158 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
161 long double mantissa = frexpl (x, &exp);
163 ASSERT (mantissa == 0.505L);
165 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
168 long double mantissa = frexpl (x, &exp);
170 ASSERT (mantissa == 0.505L);
172 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
175 long double mantissa = frexpl (x, &exp);
177 ASSERT (mantissa >= 0.5L);
178 ASSERT (mantissa < 1.0L);
179 ASSERT (mantissa == my_ldexp (x, - exp));
182 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
185 long double mantissa = frexpl (x, &exp);
187 ASSERT (mantissa == 0.866025L);
189 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
192 long double mantissa = frexpl (x, &exp);
194 ASSERT (mantissa == 0.866025L);
196 for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
199 long double mantissa = frexpl (x, &exp);
200 ASSERT (exp == i || exp == i + 1);
201 ASSERT (mantissa >= 0.5L);
202 ASSERT (mantissa < 1.0L);
203 ASSERT (mantissa == my_ldexp (x, - exp));