1 /*
2  * Copyright (c) 2003, 2012, 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  * @bug 4851638 4900189 4939441
27  * @summary Tests for {Math, StrictMath}.expm1
28  * @author Joseph D. Darcy
29  */
30 
31 /*
32  * The Taylor expansion of expxm1(x) = exp(x) -1 is
33  *
34  * 1 + x/1! + x^2/2! + x^3/3| + ... -1 =
35  *
36  * x + x^2/2! + x^3/3 + ...
37  *
38  * Therefore, for small values of x, expxm1 ~= x.
39  *
40  * For large values of x, expxm1(x) ~= exp(x)
41  *
42  * For large negative x, expxm1(x) ~= -1.
43  */
44 package test.java.lang.Math;
45 
46 import org.testng.annotations.Test;
47 import org.testng.Assert;
48 
49 public class Expm1Tests {
50 
Expm1Tests()51     private Expm1Tests() {
52     }
53 
54     static final double infinityD = Double.POSITIVE_INFINITY;
55     static final double NaNd = Double.NaN;
56 
57     @Test
testExpm1()58     public void testExpm1() {
59         double[][] testCases = {
60                 {Double.NaN, NaNd},
61                 {Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
62                 {Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
63                 {Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
64                 {Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
65                 {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
66                 {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
67                 {Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
68                 {Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
69                 {Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
70                 {Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
71                 {infinityD, infinityD},
72                 {-infinityD, -1.0},
73                 {-0.0, -0.0},
74                 {+0.0, +0.0},
75         };
76 
77         // Test special cases
78         for (double[] testCase : testCases) {
79             testExpm1CaseWithUlpDiff(testCase[0], testCase[1], 0, null);
80         }
81 
82         // For |x| < 2^-54 expm1(x) ~= x
83         for (int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
84             double d = Math.scalb(2, i);
85             testExpm1Case(d, d);
86             testExpm1Case(-d, -d);
87         }
88 
89         // For values of y where exp(y) > 2^54, expm1(x) ~= exp(x).
90         // The least such y is ln(2^54) ~= 37.42994775023705; exp(x)
91         // overflows for x > ~= 709.8
92 
93         // Use a 2-ulp error threshold to account for errors in the
94         // exp implementation; the increments of d in the loop will be
95         // exact.
96         for (double d = 37.5; d <= 709.5; d += 1.0) {
97             testExpm1CaseWithUlpDiff(d, StrictMath.exp(d), 2, null);
98         }
99 
100         // For x > 710, expm1(x) should be infinity
101         for (int i = 10; i <= Double.MAX_EXPONENT; i++) {
102             double d = Math.scalb(2, i);
103             testExpm1Case(d, infinityD);
104         }
105 
106         // By monotonicity, once the limit is reached, the
107         // implementation should return the limit for all smaller
108         // values.
109         boolean[] reachedLimit = {false, false};
110 
111         // Once exp(y) < 0.5 * ulp(1), expm1(y) ~= -1.0;
112         // The greatest such y is ln(2^-53) ~= -36.7368005696771.
113         for (double d = -36.75; d >= -127.75; d -= 1.0) {
114             testExpm1CaseWithUlpDiff(d, -1.0, 1, reachedLimit);
115         }
116 
117         for (int i = 7; i <= Double.MAX_EXPONENT; i++) {
118             double d = -Math.scalb(2, i);
119             testExpm1CaseWithUlpDiff(d, -1.0, 1, reachedLimit);
120         }
121 
122         // Test for monotonicity failures near multiples of log(2).
123         // Test two numbers before and two numbers after each chosen
124         // value; i.e.
125         //
126         // pcNeighbors[] =
127         // {nextDown(nextDown(pc)),
128         // nextDown(pc),
129         // pc,
130         // nextUp(pc),
131         // nextUp(nextUp(pc))}
132         //
133         // and we test that expm1(pcNeighbors[i]) <= expm1(pcNeighbors[i+1])
134         {
135             double[] pcNeighbors = new double[5];
136             double[] pcNeighborsExpm1 = new double[5];
137             double[] pcNeighborsStrictExpm1 = new double[5];
138 
139             for (int i = -50; i <= 50; i++) {
140                 double pc = StrictMath.log(2) * i;
141 
142                 pcNeighbors[2] = pc;
143                 pcNeighbors[1] = Math.nextDown(pc);
144                 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);
145                 pcNeighbors[3] = Math.nextUp(pc);
146                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
147 
148                 for (int j = 0; j < pcNeighbors.length; j++) {
149                     pcNeighborsExpm1[j] = Math.expm1(pcNeighbors[j]);
150                     pcNeighborsStrictExpm1[j] = StrictMath.expm1(pcNeighbors[j]);
151                 }
152 
153                 for (int j = 0; j < pcNeighborsExpm1.length - 1; j++) {
154                     if (pcNeighborsExpm1[j] > pcNeighborsExpm1[j + 1]) {
155                         Assert.fail("Monotonicity failure for Math.expm1 on " +
156                                 pcNeighbors[j] + " and " +
157                                 pcNeighbors[j + 1] + "\n\treturned " +
158                                 pcNeighborsExpm1[j] + " and " +
159                                 pcNeighborsExpm1[j + 1]);
160                     }
161 
162                     if (pcNeighborsStrictExpm1[j] > pcNeighborsStrictExpm1[j + 1]) {
163                         Assert.fail("Monotonicity failure for StrictMath.expm1 on " +
164                                 pcNeighbors[j] + " and " +
165                                 pcNeighbors[j + 1] + "\n\treturned " +
166                                 pcNeighborsStrictExpm1[j] + " and " +
167                                 pcNeighborsStrictExpm1[j + 1]);
168                     }
169 
170 
171                 }
172 
173             }
174         }
175     }
176 
testExpm1Case(double input, double expected)177     public static int testExpm1Case(double input,
178             double expected) {
179         return testExpm1CaseWithUlpDiff(input, expected, 1, null);
180     }
181 
testExpm1CaseWithUlpDiff(double input, double expected, double ulps, boolean[] reachedLimit)182     public static int testExpm1CaseWithUlpDiff(double input,
183             double expected,
184             double ulps,
185             boolean[] reachedLimit) {
186         int failures = 0;
187         double mathUlps = ulps, strictUlps = ulps;
188         double mathOutput;
189         double strictOutput;
190 
191         if (reachedLimit != null) {
192             if (reachedLimit[0]) {
193                 mathUlps = 0;
194             }
195 
196             if (reachedLimit[1]) {
197                 strictUlps = 0;
198             }
199         }
200 
201         Tests.testUlpDiffWithLowerBound("Math.expm1(double)",
202                 input, mathOutput = Math.expm1(input),
203                 expected, mathUlps, -1.0);
204         Tests.testUlpDiffWithLowerBound("StrictMath.expm1(double)",
205                 input, strictOutput = StrictMath.expm1(input),
206                 expected, strictUlps, -1.0);
207         if (reachedLimit != null) {
208             reachedLimit[0] |= (mathOutput == -1.0);
209             reachedLimit[1] |= (strictOutput == -1.0);
210         }
211 
212         return failures;
213     }
214 }
215