Path: blob/master/test/jdk/java/lang/Math/Log1pTests.java
41149 views
/*1* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @library /test/lib26* @build jdk.test.lib.RandomFactory27* @run main Log1pTests28* @bug 4851638 4939441 807867229* @summary Tests for {Math, StrictMath}.log1p (use -Dseed=X to set PRNG seed)30* @author Joseph D. Darcy31* @key randomness32*/3334import jdk.test.lib.RandomFactory;3536public class Log1pTests {37private Log1pTests(){}3839static final double infinityD = Double.POSITIVE_INFINITY;40static final double NaNd = Double.NaN;4142/**43* Formulation taken from HP-15C Advanced Functions Handbook, part44* number HP 0015-90011, p 181. This is accurate to a few ulps.45*/46static double hp15cLogp(double x) {47double u = 1.0 + x;48return (u==1.0? x : StrictMath.log(u)*x/(u-1) );49}5051/*52* The Taylor expansion of ln(1 + x) for -1 < x <= 1 is:53*54* x - x^2/2 + x^3/3 - ... -(-x^j)/j55*56* Therefore, for small values of x, log1p(x) ~= x. For large57* values of x, log1p(x) ~= log(x).58*59* Also x/(x+1) < ln(1+x) < x60*/6162static int testLog1p() {63int failures = 0;6465double [][] testCases = {66{Double.NaN, NaNd},67{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},68{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},69{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},70{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},71{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},72{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},73{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},74{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},75{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},76{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},77{Double.NEGATIVE_INFINITY, NaNd},78{-8.0, NaNd},79{-1.0, -infinityD},80{-0.0, -0.0},81{+0.0, +0.0},82{infinityD, infinityD},83};8485// Test special cases86for(int i = 0; i < testCases.length; i++) {87failures += testLog1pCaseWithUlpDiff(testCases[i][0],88testCases[i][1], 0);89}9091// For |x| < 2^-54 log1p(x) ~= x92for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {93double d = Math.scalb(2, i);94failures += testLog1pCase(d, d);95failures += testLog1pCase(-d, -d);96}9798// For x > 2^53 log1p(x) ~= log(x)99for(int i = 53; i <= Double.MAX_EXPONENT; i++) {100double d = Math.scalb(2, i);101failures += testLog1pCaseWithUlpDiff(d, StrictMath.log(d), 2.001);102}103104// Construct random values with exponents ranging from -53 to105// 52 and compare against HP-15C formula.106java.util.Random rand = RandomFactory.getRandom();107for(int i = 0; i < 1000; i++) {108double d = rand.nextDouble();109110d = Math.scalb(d, -53 - Tests.ilogb(d));111112for(int j = -53; j <= 52; j++) {113failures += testLog1pCaseWithUlpDiff(d, hp15cLogp(d), 5);114115d *= 2.0; // increase exponent by 1116}117}118119// Test for monotonicity failures near values y-1 where y ~=120// e^x. Test two numbers before and two numbers after each121// chosen value; i.e.122//123// pcNeighbors[] =124// {nextDown(nextDown(pc)),125// nextDown(pc),126// pc,127// nextUp(pc),128// nextUp(nextUp(pc))}129//130// and we test that log1p(pcNeighbors[i]) <= log1p(pcNeighbors[i+1])131{132double pcNeighbors[] = new double[5];133double pcNeighborsLog1p[] = new double[5];134double pcNeighborsStrictLog1p[] = new double[5];135136for(int i = -36; i <= 36; i++) {137double pc = StrictMath.pow(Math.E, i) - 1;138139pcNeighbors[2] = pc;140pcNeighbors[1] = Math.nextDown(pc);141pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);142pcNeighbors[3] = Math.nextUp(pc);143pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);144145for(int j = 0; j < pcNeighbors.length; j++) {146pcNeighborsLog1p[j] = Math.log1p(pcNeighbors[j]);147pcNeighborsStrictLog1p[j] = StrictMath.log1p(pcNeighbors[j]);148}149150for(int j = 0; j < pcNeighborsLog1p.length-1; j++) {151if(pcNeighborsLog1p[j] > pcNeighborsLog1p[j+1] ) {152failures++;153System.err.println("Monotonicity failure for Math.log1p on " +154pcNeighbors[j] + " and " +155pcNeighbors[j+1] + "\n\treturned " +156pcNeighborsLog1p[j] + " and " +157pcNeighborsLog1p[j+1] );158}159160if(pcNeighborsStrictLog1p[j] > pcNeighborsStrictLog1p[j+1] ) {161failures++;162System.err.println("Monotonicity failure for StrictMath.log1p on " +163pcNeighbors[j] + " and " +164pcNeighbors[j+1] + "\n\treturned " +165pcNeighborsStrictLog1p[j] + " and " +166pcNeighborsStrictLog1p[j+1] );167}168169170}171172}173}174175return failures;176}177178public static int testLog1pCase(double input,179double expected) {180return testLog1pCaseWithUlpDiff(input, expected, 1);181}182183public static int testLog1pCaseWithUlpDiff(double input,184double expected,185double ulps) {186int failures = 0;187failures += Tests.testUlpDiff("Math.lop1p(double",188input, Math.log1p(input),189expected, ulps);190failures += Tests.testUlpDiff("StrictMath.log1p(double",191input, StrictMath.log1p(input),192expected, ulps);193return failures;194}195196public static void main(String argv[]) {197int failures = 0;198199failures += testLog1p();200201if (failures > 0) {202System.err.println("Testing log1p incurred "203+ failures + " failures.");204throw new RuntimeException();205}206}207}208209210