1 /* Test of splitting a 'long double' into fraction and mantissa.
2 Copyright (C) 2007-2008 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
21 #include "printf-frexpl.h"
29 #define ASSERT(expr) \
34 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
42 exponent for 'long double' is -964. Similarly, on PowerPC machines,
43 LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
44 is -968. For exponents below that, the precision may be truncated to the
45 precision used for 'double'. */
47 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
48 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
49 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
50 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
51 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
53 # define MIN_NORMAL_EXP LDBL_MIN_EXP
54 # define MIN_SUBNORMAL_EXP (LDBL_MIN_EXP - 100)
58 my_ldexp (long double x, int d)
72 DECL_LONG_DOUBLE_ROUNDING
74 BEGIN_LONG_DOUBLE_ROUNDING ();
76 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
79 long double mantissa = printf_frexpl (x, &exp);
80 ASSERT (exp == i - 1);
81 ASSERT (mantissa == 1.0L);
83 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
86 long double mantissa = printf_frexpl (x, &exp);
87 ASSERT (exp == i - 1);
88 ASSERT (mantissa == 1.0L);
90 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
93 long double mantissa = printf_frexpl (x, &exp);
94 ASSERT (exp == LDBL_MIN_EXP - 1);
95 ASSERT (mantissa == my_ldexp (1.0L, i - LDBL_MIN_EXP));
98 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
101 long double mantissa = printf_frexpl (x, &exp);
102 ASSERT (exp == i - 1);
103 ASSERT (mantissa == 1.01L);
105 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
108 long double mantissa = printf_frexpl (x, &exp);
109 ASSERT (exp == i - 1);
110 ASSERT (mantissa == 1.01L);
112 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
115 long double mantissa = printf_frexpl (x, &exp);
116 ASSERT (exp == LDBL_MIN_EXP - 1);
117 ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
118 ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
119 ASSERT (mantissa == my_ldexp (x, - exp));
122 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
125 long double mantissa = printf_frexpl (x, &exp);
126 ASSERT (exp == i - 1);
127 ASSERT (mantissa == 1.73205L);
129 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
132 long double mantissa = printf_frexpl (x, &exp);
133 ASSERT (exp == i - 1);
134 ASSERT (mantissa == 1.73205L);
136 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
139 long double mantissa = printf_frexpl (x, &exp);
140 ASSERT (exp == LDBL_MIN_EXP - 1);
141 ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
142 ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
143 ASSERT (mantissa == my_ldexp (x, - exp));