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 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__); \
40 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
41 exponent for 'long double' is -964. Similarly, on PowerPC machines,
42 LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
43 is -968. For exponents below that, the precision may be truncated to the
44 precision used for 'double'. */
46 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
47 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
48 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
49 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
50 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
52 # define MIN_NORMAL_EXP LDBL_MIN_EXP
53 # define MIN_SUBNORMAL_EXP (LDBL_MIN_EXP - 100)
57 my_ldexp (long double x, int d)
71 DECL_LONG_DOUBLE_ROUNDING
73 BEGIN_LONG_DOUBLE_ROUNDING ();
75 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
78 long double mantissa = printf_frexpl (x, &exp);
79 ASSERT (exp == i - 1);
80 ASSERT (mantissa == 1.0L);
82 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
85 long double mantissa = printf_frexpl (x, &exp);
86 ASSERT (exp == i - 1);
87 ASSERT (mantissa == 1.0L);
89 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
92 long double mantissa = printf_frexpl (x, &exp);
93 ASSERT (exp == LDBL_MIN_EXP - 1);
94 ASSERT (mantissa == my_ldexp (1.0L, i - LDBL_MIN_EXP));
97 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
100 long double mantissa = printf_frexpl (x, &exp);
101 ASSERT (exp == i - 1);
102 ASSERT (mantissa == 1.01L);
104 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
107 long double mantissa = printf_frexpl (x, &exp);
108 ASSERT (exp == i - 1);
109 ASSERT (mantissa == 1.01L);
111 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
114 long double mantissa = printf_frexpl (x, &exp);
115 ASSERT (exp == LDBL_MIN_EXP - 1);
116 ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
117 ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
118 ASSERT (mantissa == my_ldexp (x, - exp));
121 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
124 long double mantissa = printf_frexpl (x, &exp);
125 ASSERT (exp == i - 1);
126 ASSERT (mantissa == 1.73205L);
128 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
131 long double mantissa = printf_frexpl (x, &exp);
132 ASSERT (exp == i - 1);
133 ASSERT (mantissa == 1.73205L);
135 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
138 long double mantissa = printf_frexpl (x, &exp);
139 ASSERT (exp == LDBL_MIN_EXP - 1);
140 ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
141 ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
142 ASSERT (mantissa == my_ldexp (x, - exp));