1 /*
2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @library /test/lib
27  * @build jdk.test.lib.RandomFactory
28  * @run main Log1pTests
29  * @bug 4851638 4939441 8078672
30  * @summary Tests for {Math, StrictMath}.log1p (use -Dseed=X to set PRNG seed)
31  * @author Joseph D. Darcy
32  * @key randomness
33  */
34 package test.java.lang.Math;
35 
36 import android.platform.test.annotations.LargeTest;
37 
38 import java.util.Random;
39 
40 import org.testng.annotations.Test;
41 import org.testng.Assert;
42 
43 public class Log1pTests {
44 
Log1pTests()45     private Log1pTests() {
46     }
47 
48     static final double infinityD = Double.POSITIVE_INFINITY;
49     static final double NaNd = Double.NaN;
50 
51     /**
52      * Formulation taken from HP-15C Advanced Functions Handbook, part number HP 0015-90011, p 181.
53      * This is accurate to a few ulps.
54      */
hp15cLogp(double x)55     static double hp15cLogp(double x) {
56         double u = 1.0 + x;
57         return (u == 1.0 ? x : StrictMath.log(u) * x / (u - 1));
58     }
59 
60     /*
61      * The Taylor expansion of ln(1 + x) for -1 < x <= 1 is:
62      *
63      * x - x^2/2 + x^3/3 - ... -(-x^j)/j
64      *
65      * Therefore, for small values of x, log1p(x) ~= x.  For large
66      * values of x, log1p(x) ~= log(x).
67      *
68      * Also x/(x+1) < ln(1+x) < x
69      */
70 
71     @LargeTest
72     @Test
testLog1p()73     public void testLog1p() {
74         double[][] testCases = {
75                 {Double.NaN, NaNd},
76                 {Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
77                 {Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
78                 {Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
79                 {Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
80                 {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
81                 {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
82                 {Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
83                 {Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
84                 {Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
85                 {Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
86                 {Double.NEGATIVE_INFINITY, NaNd},
87                 {-8.0, NaNd},
88                 {-1.0, -infinityD},
89                 {-0.0, -0.0},
90                 {+0.0, +0.0},
91                 {infinityD, infinityD},
92         };
93 
94         // Test special cases
95         for (double[] testCase : testCases) {
96             testLog1pCaseWithUlpDiff(testCase[0], testCase[1], 0);
97         }
98 
99         // For |x| < 2^-54 log1p(x) ~= x
100         for (int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
101             double d = Math.scalb(2, i);
102             testLog1pCase(d, d);
103             testLog1pCase(-d, -d);
104         }
105 
106         // For x > 2^53 log1p(x) ~= log(x)
107         for (int i = 53; i <= Double.MAX_EXPONENT; i++) {
108             double d = Math.scalb(2, i);
109             testLog1pCaseWithUlpDiff(d, StrictMath.log(d), 2.001);
110         }
111 
112         // Construct random values with exponents ranging from -53 to
113         // 52 and compare against HP-15C formula.
114         java.util.Random rand = new Random();
115         for (int i = 0; i < 1000; i++) {
116             double d = rand.nextDouble();
117 
118             d = Math.scalb(d, -53 - Tests.ilogb(d));
119 
120             for (int j = -53; j <= 52; j++) {
121                 testLog1pCaseWithUlpDiff(d, hp15cLogp(d), 5);
122 
123                 d *= 2.0; // increase exponent by 1
124             }
125         }
126 
127         // Test for monotonicity failures near values y-1 where y ~=
128         // e^x.  Test two numbers before and two numbers after each
129         // chosen value; i.e.
130         //
131         // pcNeighbors[] =
132         // {nextDown(nextDown(pc)),
133         // nextDown(pc),
134         // pc,
135         // nextUp(pc),
136         // nextUp(nextUp(pc))}
137         //
138         // and we test that log1p(pcNeighbors[i]) <= log1p(pcNeighbors[i+1])
139         {
140             double[] pcNeighbors = new double[5];
141             double[] pcNeighborsLog1p = new double[5];
142             double[] pcNeighborsStrictLog1p = new double[5];
143 
144             for (int i = -36; i <= 36; i++) {
145                 double pc = StrictMath.pow(Math.E, i) - 1;
146 
147                 pcNeighbors[2] = pc;
148                 pcNeighbors[1] = Math.nextDown(pc);
149                 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);
150                 pcNeighbors[3] = Math.nextUp(pc);
151                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
152 
153                 for (int j = 0; j < pcNeighbors.length; j++) {
154                     pcNeighborsLog1p[j] = Math.log1p(pcNeighbors[j]);
155                     pcNeighborsStrictLog1p[j] = StrictMath.log1p(pcNeighbors[j]);
156                 }
157 
158                 for (int j = 0; j < pcNeighborsLog1p.length - 1; j++) {
159                     if (pcNeighborsLog1p[j] > pcNeighborsLog1p[j + 1]) {
160                         Assert.fail("Monotonicity failure for Math.log1p on " +
161                                 pcNeighbors[j] + " and " +
162                                 pcNeighbors[j + 1] + "\n\treturned " +
163                                 pcNeighborsLog1p[j] + " and " +
164                                 pcNeighborsLog1p[j + 1]);
165                     }
166 
167                     if (pcNeighborsStrictLog1p[j] > pcNeighborsStrictLog1p[j + 1]) {
168                         Assert.fail("Monotonicity failure for StrictMath.log1p on " +
169                                 pcNeighbors[j] + " and " +
170                                 pcNeighbors[j + 1] + "\n\treturned " +
171                                 pcNeighborsStrictLog1p[j] + " and " +
172                                 pcNeighborsStrictLog1p[j + 1]);
173                     }
174 
175 
176                 }
177 
178             }
179         }
180     }
181 
testLog1pCase(double input, double expected)182     public static void testLog1pCase(double input, double expected) {
183         testLog1pCaseWithUlpDiff(input, expected, 1);
184     }
185 
testLog1pCaseWithUlpDiff(double input, double expected, double ulps)186     public static void testLog1pCaseWithUlpDiff(double input, double expected, double ulps) {
187         Tests.testUlpDiff("Math.lop1p(double)",
188                 input, Math.log1p(input),
189                 expected, ulps);
190         Tests.testUlpDiff("StrictMath.log1p(double)",
191                 input, StrictMath.log1p(input),
192                 expected, ulps);
193     }
194 }
195