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. */
22 #include "printf-frexpl.h"
30 #define ASSERT(expr) \
35 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. For exponents below that, the
43 precision may be truncated to the precision used for 'double'. */
45 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
46 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
48 # define MIN_NORMAL_EXP LDBL_MIN_EXP
49 # define MIN_SUBNORMAL_EXP (LDBL_MIN_EXP - 100)
53 my_ldexp (long double x, int d)
67 DECL_LONG_DOUBLE_ROUNDING
69 BEGIN_LONG_DOUBLE_ROUNDING ();
71 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
74 long double mantissa = printf_frexpl (x, &exp);
75 ASSERT (exp == i - 1);
76 ASSERT (mantissa == 1.0L);
78 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
81 long double mantissa = printf_frexpl (x, &exp);
82 ASSERT (exp == i - 1);
83 ASSERT (mantissa == 1.0L);
85 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
88 long double mantissa = printf_frexpl (x, &exp);
89 ASSERT (exp == LDBL_MIN_EXP - 1);
90 ASSERT (mantissa == my_ldexp (1.0L, i - LDBL_MIN_EXP));
93 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
96 long double mantissa = printf_frexpl (x, &exp);
97 ASSERT (exp == i - 1);
98 ASSERT (mantissa == 1.01L);
100 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
103 long double mantissa = printf_frexpl (x, &exp);
104 ASSERT (exp == i - 1);
105 ASSERT (mantissa == 1.01L);
107 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
110 long double mantissa = printf_frexpl (x, &exp);
111 ASSERT (exp == LDBL_MIN_EXP - 1);
112 ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
113 ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
114 ASSERT (mantissa == my_ldexp (x, - exp));
117 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
120 long double mantissa = printf_frexpl (x, &exp);
121 ASSERT (exp == i - 1);
122 ASSERT (mantissa == 1.73205L);
124 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
127 long double mantissa = printf_frexpl (x, &exp);
128 ASSERT (exp == i - 1);
129 ASSERT (mantissa == 1.73205L);
131 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
134 long double mantissa = printf_frexpl (x, &exp);
135 ASSERT (exp == LDBL_MIN_EXP - 1);
136 ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
137 ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
138 ASSERT (mantissa == my_ldexp (x, - exp));