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"
29 #define ASSERT(expr) if (!(expr)) abort ();
31 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
32 exponent for 'long double' is -964. For exponents below that, the
33 precision may be truncated to the precision used for 'double'. */
35 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
36 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
38 # define MIN_NORMAL_EXP LDBL_MIN_EXP
39 # define MIN_SUBNORMAL_EXP (LDBL_MIN_EXP - 100)
43 my_ldexp (long double x, int d)
57 DECL_LONG_DOUBLE_ROUNDING
59 BEGIN_LONG_DOUBLE_ROUNDING ();
61 for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
64 long double mantissa = printf_frexpl (x, &exp);
65 ASSERT (exp == i - 1);
66 ASSERT (mantissa == 1.0L);
68 for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
71 long double mantissa = printf_frexpl (x, &exp);
72 ASSERT (exp == i - 1);
73 ASSERT (mantissa == 1.0L);
75 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
78 long double mantissa = printf_frexpl (x, &exp);
79 ASSERT (exp == LDBL_MIN_EXP - 1);
80 ASSERT (mantissa == my_ldexp (1.0L, i - LDBL_MIN_EXP));
83 for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
86 long double mantissa = printf_frexpl (x, &exp);
87 ASSERT (exp == i - 1);
88 ASSERT (mantissa == 1.01L);
90 for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
93 long double mantissa = printf_frexpl (x, &exp);
94 ASSERT (exp == i - 1);
95 ASSERT (mantissa == 1.01L);
97 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
100 long double mantissa = printf_frexpl (x, &exp);
101 ASSERT (exp == LDBL_MIN_EXP - 1);
102 ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
103 ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
104 ASSERT (mantissa == my_ldexp (x, - exp));
107 for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
110 long double mantissa = printf_frexpl (x, &exp);
111 ASSERT (exp == i - 1);
112 ASSERT (mantissa == 1.73205L);
114 for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
117 long double mantissa = printf_frexpl (x, &exp);
118 ASSERT (exp == i - 1);
119 ASSERT (mantissa == 1.73205L);
121 for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
124 long double mantissa = printf_frexpl (x, &exp);
125 ASSERT (exp == LDBL_MIN_EXP - 1);
126 ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
127 ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
128 ASSERT (mantissa == my_ldexp (x, - exp));