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"
27 #define ASSERT(expr) if (!(expr)) abort ();
30 my_ldexp (double x, int d)
43 /* The use of 'volatile' guarantees that excess precision bits are dropped
44 when dealing with denormalized numbers. It is necessary on x86 systems
45 where double-floats are not IEEE compliant by default, to avoid that the
46 results become platform and compiler option dependent. 'volatile' is a
47 portable alternative to gcc's -ffloat-store option. */
50 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
53 double mantissa = printf_frexp (x, &exp);
54 ASSERT (exp == i - 1);
55 ASSERT (mantissa == 1.0);
57 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
60 double mantissa = printf_frexp (x, &exp);
61 ASSERT (exp == i - 1);
62 ASSERT (mantissa == 1.0);
64 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
67 double mantissa = printf_frexp (x, &exp);
68 ASSERT (exp == DBL_MIN_EXP - 1);
69 ASSERT (mantissa == my_ldexp (1.0, i - DBL_MIN_EXP));
72 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
75 double mantissa = printf_frexp (x, &exp);
76 ASSERT (exp == i - 1);
77 ASSERT (mantissa == 1.01);
79 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
82 double mantissa = printf_frexp (x, &exp);
83 ASSERT (exp == i - 1);
84 ASSERT (mantissa == 1.01);
86 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
89 double mantissa = printf_frexp (x, &exp);
90 ASSERT (exp == DBL_MIN_EXP - 1);
91 ASSERT (mantissa >= my_ldexp (1.0, i - DBL_MIN_EXP));
92 ASSERT (mantissa <= my_ldexp (2.0, i - DBL_MIN_EXP));
93 ASSERT (mantissa == my_ldexp (x, - exp));
96 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
99 double mantissa = printf_frexp (x, &exp);
100 ASSERT (exp == i - 1);
101 ASSERT (mantissa == 1.73205);
103 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
106 double mantissa = printf_frexp (x, &exp);
107 ASSERT (exp == i - 1);
108 ASSERT (mantissa == 1.73205);
110 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
113 double mantissa = printf_frexp (x, &exp);
114 ASSERT (exp == DBL_MIN_EXP - 1);
115 ASSERT (mantissa >= my_ldexp (1.0, i - DBL_MIN_EXP));
116 ASSERT (mantissa <= my_ldexp (2.0, i - DBL_MIN_EXP));
117 ASSERT (mantissa == my_ldexp (x, - exp));