1 /* Test of splitting a double into fraction and mantissa.
2 Copyright (C) 2007-2020 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
21 #include "printf-frexp.h"
28 my_ldexp (double x
, int d
)
41 /* The use of 'volatile' guarantees that excess precision bits are dropped
42 when dealing with denormalized numbers. It is necessary on x86 systems
43 where double-floats are not IEEE compliant by default, to avoid that the
44 results become platform and compiler option dependent. 'volatile' is a
45 portable alternative to gcc's -ffloat-store option. */
48 for (i
= 1, x
= 1.0; i
<= DBL_MAX_EXP
; i
++, x
*= 2.0)
51 double mantissa
= printf_frexp (x
, &exp
);
52 ASSERT (exp
== i
- 1);
53 ASSERT (mantissa
== 1.0);
55 for (i
= 1, x
= 1.0; i
>= DBL_MIN_EXP
; i
--, x
*= 0.5)
58 double mantissa
= printf_frexp (x
, &exp
);
59 ASSERT (exp
== i
- 1);
60 ASSERT (mantissa
== 1.0);
62 for (; i
>= DBL_MIN_EXP
- 100 && x
> 0.0; i
--, x
*= 0.5)
65 double mantissa
= printf_frexp (x
, &exp
);
66 ASSERT (exp
== DBL_MIN_EXP
- 1);
67 ASSERT (mantissa
== my_ldexp (1.0, i
- DBL_MIN_EXP
));
70 for (i
= 1, x
= 1.01; i
<= DBL_MAX_EXP
; i
++, x
*= 2.0)
73 double mantissa
= printf_frexp (x
, &exp
);
74 ASSERT (exp
== i
- 1);
75 ASSERT (mantissa
== 1.01);
77 for (i
= 1, x
= 1.01; i
>= DBL_MIN_EXP
; i
--, x
*= 0.5)
80 double mantissa
= printf_frexp (x
, &exp
);
81 ASSERT (exp
== i
- 1);
82 ASSERT (mantissa
== 1.01);
84 for (; i
>= DBL_MIN_EXP
- 100 && x
> 0.0; i
--, x
*= 0.5)
87 double mantissa
= printf_frexp (x
, &exp
);
88 ASSERT (exp
== DBL_MIN_EXP
- 1);
89 ASSERT (mantissa
>= my_ldexp (1.0, i
- DBL_MIN_EXP
));
90 ASSERT (mantissa
<= my_ldexp (2.0, i
- DBL_MIN_EXP
));
91 ASSERT (mantissa
== my_ldexp (x
, - exp
));
94 for (i
= 1, x
= 1.73205; i
<= DBL_MAX_EXP
; i
++, x
*= 2.0)
97 double mantissa
= printf_frexp (x
, &exp
);
98 ASSERT (exp
== i
- 1);
99 ASSERT (mantissa
== 1.73205);
101 for (i
= 1, x
= 1.73205; i
>= DBL_MIN_EXP
; i
--, x
*= 0.5)
104 double mantissa
= printf_frexp (x
, &exp
);
105 ASSERT (exp
== i
- 1);
106 ASSERT (mantissa
== 1.73205);
108 for (; i
>= DBL_MIN_EXP
- 100 && x
> 0.0; i
--, x
*= 0.5)
111 double mantissa
= printf_frexp (x
, &exp
);
112 ASSERT (exp
== DBL_MIN_EXP
- 1);
113 ASSERT (mantissa
>= my_ldexp (1.0, i
- DBL_MIN_EXP
));
114 ASSERT (mantissa
<= my_ldexp (2.0, i
- DBL_MIN_EXP
));
115 ASSERT (mantissa
== my_ldexp (x
, - exp
));