1 /* Test of splitting a double into fraction and mantissa.
2 Copyright (C) 2012 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/>. */
18 my_ldexp (DOUBLE x
, int d
)
37 mantissa
= FREXP (x
, &exp
);
38 ASSERT (ISNAN (mantissa
));
41 { /* Positive infinity. */
45 mantissa
= FREXP (x
, &exp
);
46 ASSERT (mantissa
== x
);
49 { /* Negative infinity. */
53 mantissa
= FREXP (x
, &exp
);
54 ASSERT (mantissa
== x
);
57 { /* Positive zero. */
61 mantissa
= FREXP (x
, &exp
);
63 ASSERT (mantissa
== x
);
64 ASSERT (!signbit (mantissa
));
67 { /* Negative zero. */
71 mantissa
= FREXP (x
, &exp
);
73 ASSERT (mantissa
== x
);
74 ASSERT (signbit (mantissa
));
77 for (i
= 1, x
= L_(1.0); i
<= MAX_EXP
; i
++, x
*= L_(2.0))
80 DOUBLE mantissa
= FREXP (x
, &exp
);
82 ASSERT (mantissa
== L_(0.5));
84 for (i
= 1, x
= L_(1.0); i
>= MIN_NORMAL_EXP
; i
--, x
*= L_(0.5))
87 DOUBLE mantissa
= FREXP (x
, &exp
);
89 ASSERT (mantissa
== L_(0.5));
91 for (; i
>= MIN_EXP
- 100 && x
> L_(0.0); i
--, x
*= L_(0.5))
94 DOUBLE mantissa
= FREXP (x
, &exp
);
96 ASSERT (mantissa
== L_(0.5));
99 for (i
= 1, x
= - L_(1.0); i
<= MAX_EXP
; i
++, x
*= L_(2.0))
102 DOUBLE mantissa
= FREXP (x
, &exp
);
104 ASSERT (mantissa
== - L_(0.5));
106 for (i
= 1, x
= - L_(1.0); i
>= MIN_NORMAL_EXP
; i
--, x
*= L_(0.5))
109 DOUBLE mantissa
= FREXP (x
, &exp
);
111 ASSERT (mantissa
== - L_(0.5));
113 for (; i
>= MIN_EXP
- 100 && x
< L_(0.0); i
--, x
*= L_(0.5))
116 DOUBLE mantissa
= FREXP (x
, &exp
);
118 ASSERT (mantissa
== - L_(0.5));
121 for (i
= 1, x
= L_(1.01); i
<= MAX_EXP
; i
++, x
*= L_(2.0))
124 DOUBLE mantissa
= FREXP (x
, &exp
);
126 ASSERT (mantissa
== L_(0.505));
128 for (i
= 1, x
= L_(1.01); i
>= MIN_NORMAL_EXP
; i
--, x
*= L_(0.5))
131 DOUBLE mantissa
= FREXP (x
, &exp
);
133 ASSERT (mantissa
== L_(0.505));
135 for (; i
>= MIN_EXP
- 100 && x
> L_(0.0); i
--, x
*= L_(0.5))
138 DOUBLE mantissa
= FREXP (x
, &exp
);
140 ASSERT (mantissa
>= L_(0.5));
141 ASSERT (mantissa
< L_(1.0));
142 ASSERT (mantissa
== my_ldexp (x
, - exp
));
145 for (i
= 1, x
= L_(1.73205); i
<= MAX_EXP
; i
++, x
*= L_(2.0))
148 DOUBLE mantissa
= FREXP (x
, &exp
);
150 ASSERT (mantissa
== L_(0.866025));
152 for (i
= 1, x
= L_(1.73205); i
>= MIN_NORMAL_EXP
; i
--, x
*= L_(0.5))
155 DOUBLE mantissa
= FREXP (x
, &exp
);
157 ASSERT (mantissa
== L_(0.866025));
159 for (; i
>= MIN_EXP
- 100 && x
> L_(0.0); i
--, x
*= L_(0.5))
162 DOUBLE mantissa
= FREXP (x
, &exp
);
163 ASSERT (exp
== i
|| exp
== i
+ 1);
164 ASSERT (mantissa
>= L_(0.5));
165 ASSERT (mantissa
< L_(1.0));
166 ASSERT (mantissa
== my_ldexp (x
, - exp
));
169 /* Randomized tests. */
170 for (i
= 0; i
< SIZEOF (RANDOM
); i
++)
172 x
= L_(20.0) * RANDOM
[i
] - L_(10.0); /* -10.0 <= x <= 10.0 */
175 DOUBLE mantissa
= FREXP (x
, &exp
);
176 ASSERT (x
== my_ldexp (mantissa
, exp
));