809N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
809N/A *
809N/A * This code is free software; you can redistribute it and/or modify it
809N/A * under the terms of the GNU General Public License version 2 only, as
809N/A * published by the Free Software Foundation.
809N/A *
809N/A * This code is distributed in the hope that it will be useful, but WITHOUT
809N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
809N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
809N/A * version 2 for more details (a copy is included in the LICENSE file that
809N/A * accompanied this code).
809N/A *
809N/A * You should have received a copy of the GNU General Public License version
809N/A * 2 along with this work; if not, write to the Free Software Foundation,
809N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
809N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
809N/A */
809N/A
809N/A/*
809N/A * @test
809N/A * @bug 4851638 4939441
809N/A * @summary Tests for {Math, StrictMath}.log1p
809N/A * @author Joseph D. Darcy
809N/A */
809N/A
809N/Aimport sun.misc.DoubleConsts;
809N/Aimport sun.misc.FpUtils;
809N/A
809N/Apublic class Log1pTests {
809N/A private Log1pTests(){}
809N/A
809N/A static final double infinityD = Double.POSITIVE_INFINITY;
809N/A static final double NaNd = Double.NaN;
809N/A
809N/A /**
809N/A * Formulation taken from HP-15C Advanced Functions Handbook, part
809N/A * number HP 0015-90011, p 181. This is accurate to a few ulps.
809N/A */
809N/A static double hp15cLogp(double x) {
809N/A double u = 1.0 + x;
809N/A return (u==1.0? x : StrictMath.log(u)*x/(u-1) );
809N/A }
809N/A
809N/A /*
809N/A * The Taylor expansion of ln(1 + x) for -1 < x <= 1 is:
809N/A *
809N/A * x - x^2/2 + x^3/3 - ... -(-x^j)/j
809N/A *
809N/A * Therefore, for small values of x, log1p(x) ~= x. For large
809N/A * values of x, log1p(x) ~= log(x).
809N/A *
809N/A * Also x/(x+1) < ln(1+x) < x
809N/A */
809N/A
809N/A static int testLog1p() {
809N/A int failures = 0;
809N/A
809N/A double [][] testCases = {
809N/A {Double.NaN, NaNd},
809N/A {Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
809N/A {Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
809N/A {Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
809N/A {Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
809N/A {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
809N/A {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
809N/A {Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
809N/A {Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
809N/A {Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
809N/A {Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
809N/A {Double.NEGATIVE_INFINITY, NaNd},
809N/A {-8.0, NaNd},
809N/A {-1.0, -infinityD},
809N/A {-0.0, -0.0},
809N/A {+0.0, +0.0},
809N/A {infinityD, infinityD},
809N/A };
809N/A
809N/A // Test special cases
809N/A for(int i = 0; i < testCases.length; i++) {
809N/A failures += testLog1pCaseWithUlpDiff(testCases[i][0],
809N/A testCases[i][1], 0);
809N/A }
809N/A
809N/A // For |x| < 2^-54 log1p(x) ~= x
809N/A for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
809N/A double d = FpUtils.scalb(2, i);
809N/A failures += testLog1pCase(d, d);
809N/A failures += testLog1pCase(-d, -d);
809N/A }
809N/A
809N/A // For x > 2^53 log1p(x) ~= log(x)
809N/A for(int i = 53; i <= DoubleConsts.MAX_EXPONENT; i++) {
809N/A double d = FpUtils.scalb(2, i);
809N/A failures += testLog1pCaseWithUlpDiff(d, StrictMath.log(d), 2.001);
809N/A }
809N/A
809N/A // Construct random values with exponents ranging from -53 to
809N/A // 52 and compare against HP-15C formula.
809N/A java.util.Random rand = new java.util.Random();
809N/A for(int i = 0; i < 1000; i++) {
809N/A double d = rand.nextDouble();
809N/A
809N/A d = FpUtils.scalb(d, -53 - FpUtils.ilogb(d));
809N/A
809N/A for(int j = -53; j <= 52; j++) {
809N/A failures += testLog1pCaseWithUlpDiff(d, hp15cLogp(d), 5);
809N/A
809N/A d *= 2.0; // increase exponent by 1
809N/A }
809N/A }
809N/A
809N/A // Test for monotonicity failures near values y-1 where y ~=
809N/A // e^x. Test two numbers before and two numbers after each
809N/A // chosen value; i.e.
809N/A //
809N/A // pcNeighbors[] =
809N/A // {nextDown(nextDown(pc)),
809N/A // nextDown(pc),
809N/A // pc,
809N/A // nextUp(pc),
809N/A // nextUp(nextUp(pc))}
809N/A //
809N/A // and we test that log1p(pcNeighbors[i]) <= log1p(pcNeighbors[i+1])
809N/A {
809N/A double pcNeighbors[] = new double[5];
809N/A double pcNeighborsLog1p[] = new double[5];
809N/A double pcNeighborsStrictLog1p[] = new double[5];
809N/A
809N/A for(int i = -36; i <= 36; i++) {
809N/A double pc = StrictMath.pow(Math.E, i) - 1;
809N/A
809N/A pcNeighbors[2] = pc;
809N/A pcNeighbors[1] = FpUtils.nextDown(pc);
809N/A pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
809N/A pcNeighbors[3] = FpUtils.nextUp(pc);
809N/A pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]);
809N/A
809N/A for(int j = 0; j < pcNeighbors.length; j++) {
809N/A pcNeighborsLog1p[j] = Math.log1p(pcNeighbors[j]);
809N/A pcNeighborsStrictLog1p[j] = StrictMath.log1p(pcNeighbors[j]);
809N/A }
809N/A
809N/A for(int j = 0; j < pcNeighborsLog1p.length-1; j++) {
809N/A if(pcNeighborsLog1p[j] > pcNeighborsLog1p[j+1] ) {
809N/A failures++;
809N/A System.err.println("Monotonicity failure for Math.log1p on " +
809N/A pcNeighbors[j] + " and " +
809N/A pcNeighbors[j+1] + "\n\treturned " +
809N/A pcNeighborsLog1p[j] + " and " +
809N/A pcNeighborsLog1p[j+1] );
809N/A }
809N/A
809N/A if(pcNeighborsStrictLog1p[j] > pcNeighborsStrictLog1p[j+1] ) {
809N/A failures++;
809N/A System.err.println("Monotonicity failure for StrictMath.log1p on " +
809N/A pcNeighbors[j] + " and " +
809N/A pcNeighbors[j+1] + "\n\treturned " +
809N/A pcNeighborsStrictLog1p[j] + " and " +
809N/A pcNeighborsStrictLog1p[j+1] );
809N/A }
809N/A
809N/A
809N/A }
809N/A
809N/A }
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A public static int testLog1pCase(double input,
809N/A double expected) {
809N/A return testLog1pCaseWithUlpDiff(input, expected, 1);
809N/A }
809N/A
809N/A public static int testLog1pCaseWithUlpDiff(double input,
809N/A double expected,
809N/A double ulps) {
809N/A int failures = 0;
809N/A failures += Tests.testUlpDiff("Math.lop1p(double",
809N/A input, Math.log1p(input),
809N/A expected, ulps);
809N/A failures += Tests.testUlpDiff("StrictMath.log1p(double",
809N/A input, StrictMath.log1p(input),
809N/A expected, ulps);
809N/A return failures;
809N/A }
809N/A
809N/A public static void main(String argv[]) {
809N/A int failures = 0;
809N/A
809N/A failures += testLog1p();
809N/A
809N/A if (failures > 0) {
809N/A System.err.println("Testing log1p incurred "
809N/A + failures + " failures.");
809N/A throw new RuntimeException();
809N/A }
809N/A }
809N/A
809N/A}