usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-ldexp.h
blob7d836ac3b8ecc73700dfb9fc3dac4240bf7c248d
1 /* Test of ldexp*() function family.
2 Copyright (C) 2007-2024 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, 2010. */
19 #include <limits.h>
21 static void
22 test_function (void)
24 int i;
25 volatile DOUBLE x;
26 volatile DOUBLE y;
28 /* A particular value. */
30 x = L_(0.6);
31 y = LDEXP (x, 0);
32 ASSERT (y >= L_(0.5999999999) && y <= L_(0.6000000001));
35 x = L_(0.6);
36 y = LDEXP (x, 1);
37 ASSERT (y >= L_(1.199999999) && y <= L_(1.200000001));
40 x = L_(0.6);
41 y = LDEXP (x, -1);
42 ASSERT (y >= L_(0.2999999999) && y <= L_(0.3000000001));
45 { /* NaN. */
46 x = NAN;
47 y = LDEXP (x, 0); ASSERT (ISNAN (y));
48 y = LDEXP (x, 5); ASSERT (ISNAN (y));
49 y = LDEXP (x, -5); ASSERT (ISNAN (y));
52 { /* Positive infinity. */
53 x = INFINITY;
54 y = LDEXP (x, 0); ASSERT (y == x);
55 y = LDEXP (x, 5); ASSERT (y == x);
56 y = LDEXP (x, -5); ASSERT (y == x);
59 { /* Negative infinity. */
60 x = - INFINITY;
61 y = LDEXP (x, 0); ASSERT (y == x);
62 y = LDEXP (x, 5); ASSERT (y == x);
63 y = LDEXP (x, -5); ASSERT (y == x);
66 { /* Positive zero. */
67 x = L_(0.0);
68 y = LDEXP (x, 0); ASSERT (y == x); ASSERT (!signbit (x));
69 y = LDEXP (x, 5); ASSERT (y == x); ASSERT (!signbit (x));
70 y = LDEXP (x, -5); ASSERT (y == x); ASSERT (!signbit (x));
73 { /* Negative zero. */
74 x = MINUS_ZERO;
75 y = LDEXP (x, 0); ASSERT (y == x); ASSERT (signbit (x));
76 y = LDEXP (x, 5); ASSERT (y == x); ASSERT (signbit (x));
77 y = LDEXP (x, -5); ASSERT (y == x); ASSERT (signbit (x));
80 { /* Positive finite number. */
81 x = L_(1.73205);
82 y = LDEXP (x, 0); ASSERT (y == x);
83 y = LDEXP (x, 5); ASSERT (y == x * L_(32.0));
84 y = LDEXP (x, -5); ASSERT (y == x * L_(0.03125));
87 { /* Negative finite number. */
88 x = - L_(20.085536923187667742);
89 y = LDEXP (x, 0); ASSERT (y == x);
90 y = LDEXP (x, 5); ASSERT (y == x * L_(32.0));
91 y = LDEXP (x, -5); ASSERT (y == x * L_(0.03125));
94 for (i = 1, x = L_(1.73205); i <= MAX_EXP; i++, x *= L_(2.0))
96 y = LDEXP (x, 0); ASSERT (y == x);
98 volatile DOUBLE expected;
99 y = LDEXP (x, 5);
100 expected = x * L_(32.0);
101 ASSERT (y == expected);
103 y = LDEXP (x, -5); ASSERT (y == x * 0.03125L);
104 y = LDEXP (x, INT_MIN); ASSERT (y == 0);
106 for (i = 1, x = L_(1.73205); i >= MIN_EXP; i--, x *= L_(0.5))
108 y = LDEXP (x, 0); ASSERT (y == x);
109 y = LDEXP (x, 5); ASSERT (y == x * L_(32.0));
110 if (i - 5 >= MIN_EXP)
112 y = LDEXP (x, -5); ASSERT (y == x * L_(0.03125));
115 for (; i >= LDBL_MIN_EXP - 100 && x > L_(0.0); i--, x *= L_(0.5))
117 y = LDEXP (x, 0); ASSERT (y == x);
118 y = LDEXP (x, 5); ASSERT (y == x * L_(32.0));
121 /* Randomized tests. */
122 for (i = 0; i < SIZEOF (RANDOM); i++)
124 int u, v;
126 x = L_(20.0) * RANDOM[i] - L_(10.0); /* -10.0 <= x <= 10.0 */
127 /* LDEXP only does rounding when it returns a denormalized number
128 or there is underflow. It doesn't happen here. */
129 for (u = -10; u <= 10; u++)
130 for (v = -10; v <= 10; v++)
131 ASSERT (LDEXP (x, u + v) == LDEXP (LDEXP (x, u), v));