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