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)
45 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
48 double mantissa = printf_frexp (x, &exp);
49 ASSERT (exp == i - 1);
50 ASSERT (mantissa == 1.0);
52 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
55 double mantissa = printf_frexp (x, &exp);
56 ASSERT (exp == i - 1);
57 ASSERT (mantissa == 1.0);
59 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
62 double mantissa = printf_frexp (x, &exp);
63 ASSERT (exp == DBL_MIN_EXP - 1);
64 ASSERT (mantissa == my_ldexp (1.0, i - DBL_MIN_EXP));
67 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
70 double mantissa = printf_frexp (x, &exp);
71 ASSERT (exp == i - 1);
72 ASSERT (mantissa == 1.01);
74 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
77 double mantissa = printf_frexp (x, &exp);
78 ASSERT (exp == i - 1);
79 ASSERT (mantissa == 1.01);
81 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
84 double mantissa = printf_frexp (x, &exp);
85 ASSERT (exp == DBL_MIN_EXP - 1);
86 ASSERT (mantissa >= my_ldexp (1.0, i - DBL_MIN_EXP));
87 ASSERT (mantissa <= my_ldexp (2.0, i - DBL_MIN_EXP));
88 ASSERT (mantissa == my_ldexp (x, - exp));
91 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
94 double mantissa = printf_frexp (x, &exp);
95 ASSERT (exp == i - 1);
96 ASSERT (mantissa == 1.73205);
98 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
101 double mantissa = printf_frexp (x, &exp);
102 ASSERT (exp == i - 1);
103 ASSERT (mantissa == 1.73205);
105 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
108 double mantissa = printf_frexp (x, &exp);
109 ASSERT (exp == DBL_MIN_EXP - 1);
110 ASSERT (mantissa >= my_ldexp (1.0, i - DBL_MIN_EXP));
111 ASSERT (mantissa <= my_ldexp (2.0, i - DBL_MIN_EXP));
112 ASSERT (mantissa == my_ldexp (x, - exp));