1 /* Test of splitting a 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-frexp.h"
27 #define ASSERT(expr) \
32 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
39 my_ldexp (double x, int d)
52 /* The use of 'volatile' guarantees that excess precision bits are dropped
53 when dealing with denormalized numbers. It is necessary on x86 systems
54 where double-floats are not IEEE compliant by default, to avoid that the
55 results become platform and compiler option dependent. 'volatile' is a
56 portable alternative to gcc's -ffloat-store option. */
59 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
62 double mantissa = printf_frexp (x, &exp);
63 ASSERT (exp == i - 1);
64 ASSERT (mantissa == 1.0);
66 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
69 double mantissa = printf_frexp (x, &exp);
70 ASSERT (exp == i - 1);
71 ASSERT (mantissa == 1.0);
73 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
76 double mantissa = printf_frexp (x, &exp);
77 ASSERT (exp == DBL_MIN_EXP - 1);
78 ASSERT (mantissa == my_ldexp (1.0, i - DBL_MIN_EXP));
81 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
84 double mantissa = printf_frexp (x, &exp);
85 ASSERT (exp == i - 1);
86 ASSERT (mantissa == 1.01);
88 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
91 double mantissa = printf_frexp (x, &exp);
92 ASSERT (exp == i - 1);
93 ASSERT (mantissa == 1.01);
95 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
98 double mantissa = printf_frexp (x, &exp);
99 ASSERT (exp == DBL_MIN_EXP - 1);
100 ASSERT (mantissa >= my_ldexp (1.0, i - DBL_MIN_EXP));
101 ASSERT (mantissa <= my_ldexp (2.0, i - DBL_MIN_EXP));
102 ASSERT (mantissa == my_ldexp (x, - exp));
105 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
108 double mantissa = printf_frexp (x, &exp);
109 ASSERT (exp == i - 1);
110 ASSERT (mantissa == 1.73205);
112 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
115 double mantissa = printf_frexp (x, &exp);
116 ASSERT (exp == i - 1);
117 ASSERT (mantissa == 1.73205);
119 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
122 double mantissa = printf_frexp (x, &exp);
123 ASSERT (exp == DBL_MIN_EXP - 1);
124 ASSERT (mantissa >= my_ldexp (1.0, i - DBL_MIN_EXP));
125 ASSERT (mantissa <= my_ldexp (2.0, i - DBL_MIN_EXP));
126 ASSERT (mantissa == my_ldexp (x, - exp));