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 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-frexp.h"
28 #define ASSERT(expr) \
33 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
40 my_ldexp (double x, int d)
53 /* The use of 'volatile' guarantees that excess precision bits are dropped
54 when dealing with denormalized numbers. It is necessary on x86 systems
55 where double-floats are not IEEE compliant by default, to avoid that the
56 results become platform and compiler option dependent. 'volatile' is a
57 portable alternative to gcc's -ffloat-store option. */
60 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
63 double mantissa = printf_frexp (x, &exp);
64 ASSERT (exp == i - 1);
65 ASSERT (mantissa == 1.0);
67 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
70 double mantissa = printf_frexp (x, &exp);
71 ASSERT (exp == i - 1);
72 ASSERT (mantissa == 1.0);
74 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
77 double mantissa = printf_frexp (x, &exp);
78 ASSERT (exp == DBL_MIN_EXP - 1);
79 ASSERT (mantissa == my_ldexp (1.0, i - DBL_MIN_EXP));
82 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
85 double mantissa = printf_frexp (x, &exp);
86 ASSERT (exp == i - 1);
87 ASSERT (mantissa == 1.01);
89 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
92 double mantissa = printf_frexp (x, &exp);
93 ASSERT (exp == i - 1);
94 ASSERT (mantissa == 1.01);
96 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
99 double mantissa = printf_frexp (x, &exp);
100 ASSERT (exp == DBL_MIN_EXP - 1);
101 ASSERT (mantissa >= my_ldexp (1.0, i - DBL_MIN_EXP));
102 ASSERT (mantissa <= my_ldexp (2.0, i - DBL_MIN_EXP));
103 ASSERT (mantissa == my_ldexp (x, - exp));
106 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
109 double mantissa = printf_frexp (x, &exp);
110 ASSERT (exp == i - 1);
111 ASSERT (mantissa == 1.73205);
113 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
116 double mantissa = printf_frexp (x, &exp);
117 ASSERT (exp == i - 1);
118 ASSERT (mantissa == 1.73205);
120 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
123 double mantissa = printf_frexp (x, &exp);
124 ASSERT (exp == DBL_MIN_EXP - 1);
125 ASSERT (mantissa >= my_ldexp (1.0, i - DBL_MIN_EXP));
126 ASSERT (mantissa <= my_ldexp (2.0, i - DBL_MIN_EXP));
127 ASSERT (mantissa == my_ldexp (x, - exp));