1 /* 2 * Copyright (c) 2003, 2015, 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 /lib/testlibrary/ 27 * @build jdk.testlibrary.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 35 import jdk.testlibrary.RandomFactory; 36 37 public class Log1pTests { 38 private Log1pTests(){} 39 40 static final double infinityD = Double.POSITIVE_INFINITY; 41 static final double NaNd = Double.NaN; 42 43 /** 44 * Formulation taken from HP-15C Advanced Functions Handbook, part 45 * number HP 0015-90011, p 181. This is accurate to a few ulps. 46 */ 47 static double hp15cLogp(double x) { 48 double u = 1.0 + x; 49 return (u==1.0? x : StrictMath.log(u)*x/(u-1) ); 50 } 51 52 /* 53 * The Taylor expansion of ln(1 + x) for -1 < x <= 1 is: 54 * 55 * x - x^2/2 + x^3/3 - ... -(-x^j)/j 56 * 57 * Therefore, for small values of x, log1p(x) ~= x. For large 58 * values of x, log1p(x) ~= log(x). 59 * 60 * Also x/(x+1) < ln(1+x) < x 61 */ 62 63 static int testLog1p() { 64 int failures = 0; 65 66 double [][] testCases = { 67 {Double.NaN, NaNd}, 68 {Double.longBitsToDouble(0x7FF0000000000001L), NaNd}, 69 {Double.longBitsToDouble(0xFFF0000000000001L), NaNd}, 70 {Double.longBitsToDouble(0x7FF8555555555555L), NaNd}, 71 {Double.longBitsToDouble(0xFFF8555555555555L), NaNd}, 72 {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd}, 73 {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd}, 74 {Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd}, 75 {Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd}, 76 {Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd}, 77 {Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd}, 78 {Double.NEGATIVE_INFINITY, NaNd}, 79 {-8.0, NaNd}, 80 {-1.0, -infinityD}, 81 {-0.0, -0.0}, 82 {+0.0, +0.0}, 83 {infinityD, infinityD}, 84 }; 85 86 // Test special cases 87 for(int i = 0; i < testCases.length; i++) { 88 failures += testLog1pCaseWithUlpDiff(testCases[i][0], 89 testCases[i][1], 0); 90 } 91 92 // For |x| < 2^-54 log1p(x) ~= x 93 for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) { 94 double d = Math.scalb(2, i); 95 failures += testLog1pCase(d, d); 96 failures += testLog1pCase(-d, -d); 97 } 98 99 // For x > 2^53 log1p(x) ~= log(x) 100 for(int i = 53; i <= Double.MAX_EXPONENT; i++) { 101 double d = Math.scalb(2, i); 102 failures += testLog1pCaseWithUlpDiff(d, StrictMath.log(d), 2.001); 103 } 104 105 // Construct random values with exponents ranging from -53 to 106 // 52 and compare against HP-15C formula. 107 java.util.Random rand = RandomFactory.getRandom(); 108 for(int i = 0; i < 1000; i++) { 109 double d = rand.nextDouble(); 110 111 d = Math.scalb(d, -53 - Tests.ilogb(d)); 112 113 for(int j = -53; j <= 52; j++) { 114 failures += testLog1pCaseWithUlpDiff(d, hp15cLogp(d), 5); 115 116 d *= 2.0; // increase exponent by 1 117 } 118 } 119 120 // Test for monotonicity failures near values y-1 where y ~= 121 // e^x. Test two numbers before and two numbers after each 122 // chosen value; i.e. 123 // 124 // pcNeighbors[] = 125 // {nextDown(nextDown(pc)), 126 // nextDown(pc), 127 // pc, 128 // nextUp(pc), 129 // nextUp(nextUp(pc))} 130 // 131 // and we test that log1p(pcNeighbors[i]) <= log1p(pcNeighbors[i+1]) 132 { 133 double pcNeighbors[] = new double[5]; 134 double pcNeighborsLog1p[] = new double[5]; 135 double pcNeighborsStrictLog1p[] = new double[5]; 136 137 for(int i = -36; i <= 36; i++) { 138 double pc = StrictMath.pow(Math.E, i) - 1; 139 140 pcNeighbors[2] = pc; 141 pcNeighbors[1] = Math.nextDown(pc); 142 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]); 143 pcNeighbors[3] = Math.nextUp(pc); 144 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]); 145 146 for(int j = 0; j < pcNeighbors.length; j++) { 147 pcNeighborsLog1p[j] = Math.log1p(pcNeighbors[j]); 148 pcNeighborsStrictLog1p[j] = StrictMath.log1p(pcNeighbors[j]); 149 } 150 151 for(int j = 0; j < pcNeighborsLog1p.length-1; j++) { 152 if(pcNeighborsLog1p[j] > pcNeighborsLog1p[j+1] ) { 153 failures++; 154 System.err.println("Monotonicity failure for Math.log1p on " + 155 pcNeighbors[j] + " and " + 156 pcNeighbors[j+1] + "\n\treturned " + 157 pcNeighborsLog1p[j] + " and " + 158 pcNeighborsLog1p[j+1] ); 159 } 160 161 if(pcNeighborsStrictLog1p[j] > pcNeighborsStrictLog1p[j+1] ) { 162 failures++; 163 System.err.println("Monotonicity failure for StrictMath.log1p on " + 164 pcNeighbors[j] + " and " + 165 pcNeighbors[j+1] + "\n\treturned " + 166 pcNeighborsStrictLog1p[j] + " and " + 167 pcNeighborsStrictLog1p[j+1] ); 168 } 169 170 171 } 172 173 } 174 } 175 176 return failures; 177 } 178 179 public static int testLog1pCase(double input, 180 double expected) { 181 return testLog1pCaseWithUlpDiff(input, expected, 1); 182 } 183 184 public static int testLog1pCaseWithUlpDiff(double input, 185 double expected, 186 double ulps) { 187 int failures = 0; 188 failures += Tests.testUlpDiff("Math.lop1p(double", 189 input, Math.log1p(input), 190 expected, ulps); 191 failures += Tests.testUlpDiff("StrictMath.log1p(double", 192 input, StrictMath.log1p(input), 193 expected, ulps); 194 return failures; 195 } 196 197 public static void main(String argv[]) { 198 int failures = 0; 199 200 failures += testLog1p(); 201 202 if (failures > 0) { 203 System.err.println("Testing log1p incurred " 204 + failures + " failures."); 205 throw new RuntimeException(); 206 } 207 } 208 }