log* tests: More tests.
[gnulib.git] / tests / test-log.h
1 /* Test of log*() function family.
2    Copyright (C) 2012 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 <http://www.gnu.org/licenses/>.  */
16
17 static void
18 test_function (void)
19 {
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));
31
32   {
33     DOUBLE z = LOG (L_(0.0));
34     ASSERT (z == - HUGEVAL);
35   }
36   {
37     DOUBLE z = LOG (MINUS_ZERO);
38     ASSERT (z == - HUGEVAL);
39   }
40
41   /* Randomized tests.  */
42   {
43     /* Error bound, in ulps.  */
44     const DOUBLE err_bound =
45       (sizeof (DOUBLE) == sizeof (long double) ?
46 #if defined __i386__ && defined __FreeBSD__
47        /* On FreeBSD/x86 6.4, the 'long double' type really has only 53 bits of
48           precision in the compiler but 64 bits of precision at runtime.  See
49           <http://lists.gnu.org/archive/html/bug-gnulib/2008-07/msg00063.html>.
50           The compiler has truncated all 'long double' literals in logl.c to
51           53 bits of precision.  */
52        L_(40.0)
53 #else
54        L_(3.0)
55 #endif
56        : L_(3.0));
57
58     for (i = 0; i < SIZEOF (RANDOM); i++)
59       {
60         DOUBLE x = L_(16.0) * RANDOM[i] + L_(1.0); /* 1.0 <= x <= 17.0 */
61         DOUBLE y = LOG (x);
62         DOUBLE z = LOG (L_(1.0) / x);
63         DOUBLE err = y + z;
64         ASSERT (y >= L_(0.0));
65         ASSERT (z <= L_(0.0));
66         ASSERT (err > - err_bound / TWO_MANT_DIG
67                 && err < err_bound / TWO_MANT_DIG);
68       }
69   }
70
71   {
72     /* Error bound, in ulps.  */
73     const DOUBLE err_bound =
74       (sizeof (DOUBLE) == sizeof (long double) ?
75 #if defined __i386__ && defined __FreeBSD__
76        /* On FreeBSD/x86 6.4, the 'long double' type really has only 53 bits of
77           precision in the compiler but 64 bits of precision at runtime.  See
78           <http://lists.gnu.org/archive/html/bug-gnulib/2008-07/msg00063.html>.
79           The compiler has truncated all 'long double' literals in logl.c to
80           53 bits of precision.  */
81        L_(85.0)
82 #else
83        L_(7.0)
84 #endif
85        : L_(7.0));
86
87     for (i = 0; i < SIZEOF (RANDOM) / 5; i++)
88       for (j = 0; j < SIZEOF (RANDOM) / 5; j++)
89         {
90           DOUBLE x = L_(17.0) / (L_(16.0) - L_(15.0) * RANDOM[i]) - L_(1.0);
91           DOUBLE y = L_(17.0) / (L_(16.0) - L_(15.0) * RANDOM[j]) - L_(1.0);
92           /* 1/16 <= x,y <= 16 */
93           DOUBLE z = L_(1.0) / (x * y);
94           /* Approximately  x * y * z = 1.  */
95           DOUBLE err = LOG (x) + LOG (y) + LOG (z);
96           ASSERT (err > - err_bound / TWO_MANT_DIG
97                   && err < err_bound / TWO_MANT_DIG);
98         }
99   }
100 }
101
102 volatile DOUBLE x;
103 DOUBLE y;