annotate tests/test-log2.h @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents 10eb9086bea0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16677
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test of log2*() function family.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
16677
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16677
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 static void
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 test_function (void)
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 {
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 int i;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 int j;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 const DOUBLE TWO_MANT_DIG =
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 /* Assume MANT_DIG <= 5 * 31.
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 Use the identity
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 /* Pole. */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 ASSERT (LOG2 (L_(0.0)) == - HUGEVAL);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 ASSERT (LOG2 (MINUS_ZERO) == - HUGEVAL);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 /* Integral values. */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 {
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 DOUBLE x = L_(1.0);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 DOUBLE y = LOG2 (x);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 ASSERT (y == L_(0.0));
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 }
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 {
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 int e;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 DOUBLE x;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 DOUBLE y;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 for (e = 0, x = L_(0.0), y = L_(1.0);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 e <= MAX_EXP - 1;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 e++, x = x + L_(1.0), y = y * L_(2.0))
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 {
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 /* Invariant: x = e, y = 2^e. */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 DOUBLE z = LOG2 (y);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 ASSERT (z == x);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 }
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 }
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 {
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 int e;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 DOUBLE x;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 DOUBLE y;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 for (e = 0, x = L_(0.0), y = L_(1.0);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 e >= MIN_EXP - 1;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 e--, x = x - L_(1.0), y = y * L_(0.5))
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 {
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 /* Invariant: x = e, y = 2^e. */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 DOUBLE z = LOG2 (y);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 ASSERT (z == x);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 }
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 }
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 /* Randomized tests. */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 {
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 /* Error bound, in ulps. */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 const DOUBLE err_bound =
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 (sizeof (DOUBLE) > sizeof (double) ?
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 #if defined __i386__ && defined __FreeBSD__
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 /* On FreeBSD/x86 6.4, the 'long double' type really has only 53 bits of
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 precision in the compiler but 64 bits of precision at runtime. See
19439
8bfc20b57265 maint: shorten https://lists.gnu.org/archive/html/... links
Jim Meyering <meyering@fb.com>
parents: 19192
diff changeset
77 <https://lists.gnu.org/r/bug-gnulib/2008-07/msg00063.html>.
16677
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 The compiler has truncated all 'long double' literals in log2l.c to
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 53 bits of precision. */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 L_(8193.0)
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 #else
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 L_(5.0)
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 #endif
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 : L_(5.0));
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 for (i = 0; i < SIZEOF (RANDOM); i++)
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 {
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 DOUBLE x = L_(16.0) * RANDOM[i] + L_(1.0); /* 1.0 <= x <= 17.0 */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 DOUBLE y = LOG2 (x);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 DOUBLE z = LOG2 (L_(1.0) / x);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 DOUBLE err = y + z;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 ASSERT (y >= L_(0.0));
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 ASSERT (z <= L_(0.0));
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 ASSERT (err > - err_bound / TWO_MANT_DIG
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 && err < err_bound / TWO_MANT_DIG);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 }
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 }
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 {
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 /* Error bound, in ulps. */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 const DOUBLE err_bound =
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 (sizeof (DOUBLE) > sizeof (double) ?
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 #if defined __i386__ && defined __FreeBSD__
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 /* On FreeBSD/x86 6.4, the 'long double' type really has only 53 bits of
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 precision in the compiler but 64 bits of precision at runtime. See
19439
8bfc20b57265 maint: shorten https://lists.gnu.org/archive/html/... links
Jim Meyering <meyering@fb.com>
parents: 19192
diff changeset
106 <https://lists.gnu.org/r/bug-gnulib/2008-07/msg00063.html>.
16677
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 The compiler has truncated all 'long double' literals in log2l.c to
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 53 bits of precision. */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 L_(8193.0)
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 #else
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 L_(9.0)
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 #endif
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 : L_(9.0));
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 for (i = 0; i < SIZEOF (RANDOM) / 5; i++)
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 for (j = 0; j < SIZEOF (RANDOM) / 5; j++)
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 {
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 DOUBLE x = L_(17.0) / (L_(16.0) - L_(15.0) * RANDOM[i]) - L_(1.0);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 DOUBLE y = L_(17.0) / (L_(16.0) - L_(15.0) * RANDOM[j]) - L_(1.0);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 /* 1/16 <= x,y <= 16 */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 DOUBLE z = L_(1.0) / (x * y);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 /* Approximately x * y * z = 1. */
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 DOUBLE err = LOG2 (x) + LOG2 (y) + LOG2 (z);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 ASSERT (err > - err_bound / TWO_MANT_DIG
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 && err < err_bound / TWO_MANT_DIG);
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 }
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 }
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 }
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 volatile DOUBLE x;
05117c6c2f05 Tests for module 'log2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 DOUBLE y;