1 /* Test of ldexp*() function family.
2 Copyright (C) 2007-2021 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007, 2010. */
18
19 static void
test_function(void)20 test_function (void)
21 {
22 int i;
23 volatile DOUBLE x;
24 volatile DOUBLE y;
25
26 /* A particular value. */
27 {
28 x = L_(0.6);
29 y = LDEXP (x, 0);
30 ASSERT (y >= L_(0.5999999999) && y <= L_(0.6000000001));
31 }
32 {
33 x = L_(0.6);
34 y = LDEXP (x, 1);
35 ASSERT (y >= L_(1.199999999) && y <= L_(1.200000001));
36 }
37 {
38 x = L_(0.6);
39 y = LDEXP (x, -1);
40 ASSERT (y >= L_(0.2999999999) && y <= L_(0.3000000001));
41 }
42
43 { /* NaN. */
44 x = NAN;
45 y = LDEXP (x, 0); ASSERT (ISNAN (y));
46 y = LDEXP (x, 5); ASSERT (ISNAN (y));
47 y = LDEXP (x, -5); ASSERT (ISNAN (y));
48 }
49
50 { /* Positive infinity. */
51 x = INFINITY;
52 y = LDEXP (x, 0); ASSERT (y == x);
53 y = LDEXP (x, 5); ASSERT (y == x);
54 y = LDEXP (x, -5); ASSERT (y == x);
55 }
56
57 { /* Negative infinity. */
58 x = - INFINITY;
59 y = LDEXP (x, 0); ASSERT (y == x);
60 y = LDEXP (x, 5); ASSERT (y == x);
61 y = LDEXP (x, -5); ASSERT (y == x);
62 }
63
64 { /* Positive zero. */
65 x = L_(0.0);
66 y = LDEXP (x, 0); ASSERT (y == x); ASSERT (!signbit (x));
67 y = LDEXP (x, 5); ASSERT (y == x); ASSERT (!signbit (x));
68 y = LDEXP (x, -5); ASSERT (y == x); ASSERT (!signbit (x));
69 }
70
71 { /* Negative zero. */
72 x = MINUS_ZERO;
73 y = LDEXP (x, 0); ASSERT (y == x); ASSERT (signbit (x));
74 y = LDEXP (x, 5); ASSERT (y == x); ASSERT (signbit (x));
75 y = LDEXP (x, -5); ASSERT (y == x); ASSERT (signbit (x));
76 }
77
78 { /* Positive finite number. */
79 x = L_(1.73205);
80 y = LDEXP (x, 0); ASSERT (y == x);
81 y = LDEXP (x, 5); ASSERT (y == x * L_(32.0));
82 y = LDEXP (x, -5); ASSERT (y == x * L_(0.03125));
83 }
84
85 { /* Negative finite number. */
86 x = - L_(20.085536923187667742);
87 y = LDEXP (x, 0); ASSERT (y == x);
88 y = LDEXP (x, 5); ASSERT (y == x * L_(32.0));
89 y = LDEXP (x, -5); ASSERT (y == x * L_(0.03125));
90 }
91
92 for (i = 1, x = L_(1.73205); i <= MAX_EXP; i++, x *= L_(2.0))
93 {
94 y = LDEXP (x, 0); ASSERT (y == x);
95 {
96 volatile DOUBLE expected;
97 y = LDEXP (x, 5);
98 expected = x * L_(32.0);
99 ASSERT (y == expected);
100 }
101 y = LDEXP (x, -5); ASSERT (y == x * 0.03125L);
102 }
103 for (i = 1, x = L_(1.73205); i >= MIN_EXP; i--, x *= L_(0.5))
104 {
105 y = LDEXP (x, 0); ASSERT (y == x);
106 y = LDEXP (x, 5); ASSERT (y == x * L_(32.0));
107 if (i - 5 >= MIN_EXP)
108 {
109 y = LDEXP (x, -5); ASSERT (y == x * L_(0.03125));
110 }
111 }
112 for (; i >= LDBL_MIN_EXP - 100 && x > L_(0.0); i--, x *= L_(0.5))
113 {
114 y = LDEXP (x, 0); ASSERT (y == x);
115 y = LDEXP (x, 5); ASSERT (y == x * L_(32.0));
116 }
117
118 /* Randomized tests. */
119 for (i = 0; i < SIZEOF (RANDOM); i++)
120 {
121 int u, v;
122
123 x = L_(20.0) * RANDOM[i] - L_(10.0); /* -10.0 <= x <= 10.0 */
124 /* LDEXP only does rounding when it returns a denormalized number
125 or there is underflow. It doesn't happen here. */
126 for (u = -10; u <= 10; u++)
127 for (v = -10; v <= 10; v++)
128 ASSERT (LDEXP (x, u + v) == LDEXP (LDEXP (x, u), v));
129 }
130 }
131