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 4900189 4939441
809N/A * @summary Tests for {Math, StrictMath}.expm1
809N/A * @author Joseph D. Darcy
809N/A */
809N/A
809N/Aimport sun.misc.DoubleConsts;
809N/Aimport sun.misc.FpUtils;
809N/A
809N/A/*
809N/A * The Taylor expansion of expxm1(x) = exp(x) -1 is
809N/A *
809N/A * 1 + x/1! + x^2/2! + x^3/3| + ... -1 =
809N/A *
809N/A * x + x^2/2! + x^3/3 + ...
809N/A *
809N/A * Therefore, for small values of x, expxm1 ~= x.
809N/A *
809N/A * For large values of x, expxm1(x) ~= exp(x)
809N/A *
809N/A * For large negative x, expxm1(x) ~= -1.
809N/A */
809N/A
809N/Apublic class Expm1Tests {
809N/A
809N/A private Expm1Tests(){}
809N/A
809N/A static final double infinityD = Double.POSITIVE_INFINITY;
809N/A static final double NaNd = Double.NaN;
809N/A
809N/A static int testExpm1() {
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 {infinityD, infinityD},
809N/A {-infinityD, -1.0},
809N/A {-0.0, -0.0},
809N/A {+0.0, +0.0},
809N/A };
809N/A
809N/A // Test special cases
809N/A for(int i = 0; i < testCases.length; i++) {
809N/A failures += testExpm1CaseWithUlpDiff(testCases[i][0],
809N/A testCases[i][1], 0, null);
809N/A }
809N/A
809N/A
809N/A // For |x| < 2^-54 expm1(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 += testExpm1Case(d, d);
809N/A failures += testExpm1Case(-d, -d);
809N/A }
809N/A
809N/A
809N/A // For values of y where exp(y) > 2^54, expm1(x) ~= exp(x).
809N/A // The least such y is ln(2^54) ~= 37.42994775023705; exp(x)
809N/A // overflows for x > ~= 709.8
809N/A
809N/A // Use a 2-ulp error threshold to account for errors in the
809N/A // exp implementation; the increments of d in the loop will be
809N/A // exact.
809N/A for(double d = 37.5; d <= 709.5; d += 1.0) {
809N/A failures += testExpm1CaseWithUlpDiff(d, StrictMath.exp(d), 2, null);
809N/A }
809N/A
809N/A // For x > 710, expm1(x) should be infinity
809N/A for(int i = 10; i <= DoubleConsts.MAX_EXPONENT; i++) {
809N/A double d = FpUtils.scalb(2, i);
809N/A failures += testExpm1Case(d, infinityD);
809N/A }
809N/A
809N/A // By monotonicity, once the limit is reached, the
809N/A // implemenation should return the limit for all smaller
809N/A // values.
809N/A boolean reachedLimit [] = {false, false};
809N/A
809N/A // Once exp(y) < 0.5 * ulp(1), expm1(y) ~= -1.0;
809N/A // The greatest such y is ln(2^-53) ~= -36.7368005696771.
809N/A for(double d = -36.75; d >= -127.75; d -= 1.0) {
809N/A failures += testExpm1CaseWithUlpDiff(d, -1.0, 1,
809N/A reachedLimit);
809N/A }
809N/A
809N/A for(int i = 7; i <= DoubleConsts.MAX_EXPONENT; i++) {
809N/A double d = -FpUtils.scalb(2, i);
809N/A failures += testExpm1CaseWithUlpDiff(d, -1.0, 1, reachedLimit);
809N/A }
809N/A
809N/A // Test for monotonicity failures near multiples of log(2).
809N/A // Test two numbers before and two numbers after each chosen
809N/A // 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 expm1(pcNeighbors[i]) <= expm1(pcNeighbors[i+1])
809N/A {
809N/A double pcNeighbors[] = new double[5];
809N/A double pcNeighborsExpm1[] = new double[5];
809N/A double pcNeighborsStrictExpm1[] = new double[5];
809N/A
809N/A for(int i = -50; i <= 50; i++) {
809N/A double pc = StrictMath.log(2)*i;
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 pcNeighborsExpm1[j] = Math.expm1(pcNeighbors[j]);
809N/A pcNeighborsStrictExpm1[j] = StrictMath.expm1(pcNeighbors[j]);
809N/A }
809N/A
809N/A for(int j = 0; j < pcNeighborsExpm1.length-1; j++) {
809N/A if(pcNeighborsExpm1[j] > pcNeighborsExpm1[j+1] ) {
809N/A failures++;
809N/A System.err.println("Monotonicity failure for Math.expm1 on " +
809N/A pcNeighbors[j] + " and " +
809N/A pcNeighbors[j+1] + "\n\treturned " +
809N/A pcNeighborsExpm1[j] + " and " +
809N/A pcNeighborsExpm1[j+1] );
809N/A }
809N/A
809N/A if(pcNeighborsStrictExpm1[j] > pcNeighborsStrictExpm1[j+1] ) {
809N/A failures++;
809N/A System.err.println("Monotonicity failure for StrictMath.expm1 on " +
809N/A pcNeighbors[j] + " and " +
809N/A pcNeighbors[j+1] + "\n\treturned " +
809N/A pcNeighborsStrictExpm1[j] + " and " +
809N/A pcNeighborsStrictExpm1[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 testExpm1Case(double input,
809N/A double expected) {
809N/A return testExpm1CaseWithUlpDiff(input, expected, 1, null);
809N/A }
809N/A
809N/A public static int testExpm1CaseWithUlpDiff(double input,
809N/A double expected,
809N/A double ulps,
809N/A boolean [] reachedLimit) {
809N/A int failures = 0;
809N/A double mathUlps = ulps, strictUlps = ulps;
809N/A double mathOutput;
809N/A double strictOutput;
809N/A
809N/A if (reachedLimit != null) {
809N/A if (reachedLimit[0])
809N/A mathUlps = 0;
809N/A
809N/A if (reachedLimit[1])
809N/A strictUlps = 0;
809N/A }
809N/A
809N/A failures += Tests.testUlpDiffWithLowerBound("Math.expm1(double)",
809N/A input, mathOutput=Math.expm1(input),
809N/A expected, mathUlps, -1.0);
809N/A failures += Tests.testUlpDiffWithLowerBound("StrictMath.expm1(double)",
809N/A input, strictOutput=StrictMath.expm1(input),
809N/A expected, strictUlps, -1.0);
809N/A if (reachedLimit != null) {
809N/A reachedLimit[0] |= (mathOutput == -1.0);
809N/A reachedLimit[1] |= (strictOutput == -1.0);
809N/A }
809N/A
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 += testExpm1();
809N/A
809N/A if (failures > 0) {
809N/A System.err.println("Testing expm1 incurred "
809N/A + failures + " failures.");
809N/A throw new RuntimeException();
809N/A }
809N/A }
809N/A}