Path: blob/master/test/jdk/java/lang/Math/Expm1Tests.java
41149 views
/*1* Copyright (c) 2003, 2012, 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* @bug 4851638 4900189 493944126* @summary Tests for {Math, StrictMath}.expm127* @author Joseph D. Darcy28*/2930/*31* The Taylor expansion of expxm1(x) = exp(x) -1 is32*33* 1 + x/1! + x^2/2! + x^3/3| + ... -1 =34*35* x + x^2/2! + x^3/3 + ...36*37* Therefore, for small values of x, expxm1 ~= x.38*39* For large values of x, expxm1(x) ~= exp(x)40*41* For large negative x, expxm1(x) ~= -1.42*/4344public class Expm1Tests {4546private Expm1Tests(){}4748static final double infinityD = Double.POSITIVE_INFINITY;49static final double NaNd = Double.NaN;5051static int testExpm1() {52int failures = 0;5354double [][] testCases = {55{Double.NaN, NaNd},56{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},57{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},58{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},59{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},60{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},61{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},62{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},63{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},64{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},65{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},66{infinityD, infinityD},67{-infinityD, -1.0},68{-0.0, -0.0},69{+0.0, +0.0},70};7172// Test special cases73for(int i = 0; i < testCases.length; i++) {74failures += testExpm1CaseWithUlpDiff(testCases[i][0],75testCases[i][1], 0, null);76}777879// For |x| < 2^-54 expm1(x) ~= x80for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {81double d = Math.scalb(2, i);82failures += testExpm1Case(d, d);83failures += testExpm1Case(-d, -d);84}858687// For values of y where exp(y) > 2^54, expm1(x) ~= exp(x).88// The least such y is ln(2^54) ~= 37.42994775023705; exp(x)89// overflows for x > ~= 709.89091// Use a 2-ulp error threshold to account for errors in the92// exp implementation; the increments of d in the loop will be93// exact.94for(double d = 37.5; d <= 709.5; d += 1.0) {95failures += testExpm1CaseWithUlpDiff(d, StrictMath.exp(d), 2, null);96}9798// For x > 710, expm1(x) should be infinity99for(int i = 10; i <= Double.MAX_EXPONENT; i++) {100double d = Math.scalb(2, i);101failures += testExpm1Case(d, infinityD);102}103104// By monotonicity, once the limit is reached, the105// implemenation should return the limit for all smaller106// values.107boolean reachedLimit [] = {false, false};108109// Once exp(y) < 0.5 * ulp(1), expm1(y) ~= -1.0;110// The greatest such y is ln(2^-53) ~= -36.7368005696771.111for(double d = -36.75; d >= -127.75; d -= 1.0) {112failures += testExpm1CaseWithUlpDiff(d, -1.0, 1,113reachedLimit);114}115116for(int i = 7; i <= Double.MAX_EXPONENT; i++) {117double d = -Math.scalb(2, i);118failures += testExpm1CaseWithUlpDiff(d, -1.0, 1, reachedLimit);119}120121// Test for monotonicity failures near multiples of log(2).122// Test two numbers before and two numbers after each chosen123// value; i.e.124//125// pcNeighbors[] =126// {nextDown(nextDown(pc)),127// nextDown(pc),128// pc,129// nextUp(pc),130// nextUp(nextUp(pc))}131//132// and we test that expm1(pcNeighbors[i]) <= expm1(pcNeighbors[i+1])133{134double pcNeighbors[] = new double[5];135double pcNeighborsExpm1[] = new double[5];136double pcNeighborsStrictExpm1[] = new double[5];137138for(int i = -50; i <= 50; i++) {139double pc = StrictMath.log(2)*i;140141pcNeighbors[2] = pc;142pcNeighbors[1] = Math.nextDown(pc);143pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);144pcNeighbors[3] = Math.nextUp(pc);145pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);146147for(int j = 0; j < pcNeighbors.length; j++) {148pcNeighborsExpm1[j] = Math.expm1(pcNeighbors[j]);149pcNeighborsStrictExpm1[j] = StrictMath.expm1(pcNeighbors[j]);150}151152for(int j = 0; j < pcNeighborsExpm1.length-1; j++) {153if(pcNeighborsExpm1[j] > pcNeighborsExpm1[j+1] ) {154failures++;155System.err.println("Monotonicity failure for Math.expm1 on " +156pcNeighbors[j] + " and " +157pcNeighbors[j+1] + "\n\treturned " +158pcNeighborsExpm1[j] + " and " +159pcNeighborsExpm1[j+1] );160}161162if(pcNeighborsStrictExpm1[j] > pcNeighborsStrictExpm1[j+1] ) {163failures++;164System.err.println("Monotonicity failure for StrictMath.expm1 on " +165pcNeighbors[j] + " and " +166pcNeighbors[j+1] + "\n\treturned " +167pcNeighborsStrictExpm1[j] + " and " +168pcNeighborsStrictExpm1[j+1] );169}170171172}173174}175}176177return failures;178}179180public static int testExpm1Case(double input,181double expected) {182return testExpm1CaseWithUlpDiff(input, expected, 1, null);183}184185public static int testExpm1CaseWithUlpDiff(double input,186double expected,187double ulps,188boolean [] reachedLimit) {189int failures = 0;190double mathUlps = ulps, strictUlps = ulps;191double mathOutput;192double strictOutput;193194if (reachedLimit != null) {195if (reachedLimit[0])196mathUlps = 0;197198if (reachedLimit[1])199strictUlps = 0;200}201202failures += Tests.testUlpDiffWithLowerBound("Math.expm1(double)",203input, mathOutput=Math.expm1(input),204expected, mathUlps, -1.0);205failures += Tests.testUlpDiffWithLowerBound("StrictMath.expm1(double)",206input, strictOutput=StrictMath.expm1(input),207expected, strictUlps, -1.0);208if (reachedLimit != null) {209reachedLimit[0] |= (mathOutput == -1.0);210reachedLimit[1] |= (strictOutput == -1.0);211}212213return failures;214}215216public static void main(String argv[]) {217int failures = 0;218219failures += testExpm1();220221if (failures > 0) {222System.err.println("Testing expm1 incurred "223+ failures + " failures.");224throw new RuntimeException();225}226}227}228229230