openat: don’t close (-1)
[gnulib.git] / tests / test-sqrt.h
blobedf9383cf47d5511955cce7016de0e2e59bb2848
1 /* Test of sqrt*() function family.
2 Copyright (C) 2012-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 static void
18 test_function (void)
20 int i;
21 int j;
22 const DOUBLE TWO_MANT_DIG =
23 /* Assume MANT_DIG <= 5 * 31.
24 Use the identity
25 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
26 (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
27 * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
28 * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
29 * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
30 * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
32 /* Randomized tests. */
33 for (i = 0; i < SIZEOF (RANDOM); i++)
35 DOUBLE x = L_(16.0) * RANDOM[i]; /* 0.0 <= x <= 16.0 */
36 DOUBLE y = SQRT (x);
37 DOUBLE err = y * y - x;
38 ASSERT (y >= L_(0.0));
39 ASSERT (err > - L_(16.0) / TWO_MANT_DIG
40 && err < L_(16.0) / TWO_MANT_DIG);
43 for (i = 0; i < SIZEOF (RANDOM) / 4; i++)
44 for (j = 0; j < SIZEOF (RANDOM) / 4; j++)
46 DOUBLE x = L_(16.0) * RANDOM[i]; /* 0.0 <= x <= 16.0 */
47 DOUBLE y = L_(16.0) * RANDOM[j]; /* 0.0 <= y <= 16.0 */
48 if (x > L_(0.0) && y > L_(0.0))
50 DOUBLE z = L_(1.0) / (x * y);
51 /* Approximately x * y * z = 1. */
52 DOUBLE p = SQRT (x) * SQRT (y) * SQRT (z);
53 ASSERT (p > L_(1.0) - L_(4.0) / TWO_MANT_DIG
54 && p < L_(1.0) + L_(4.0) / TWO_MANT_DIG);
59 volatile DOUBLE x;
60 DOUBLE y;