Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Math/Log1pTests.java
38812 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 493944126* @summary Tests for {Math, StrictMath}.log1p27* @author Joseph D. Darcy28* @key randomness29*/3031import sun.misc.DoubleConsts;32import sun.misc.FpUtils;3334public class Log1pTests {35private Log1pTests(){}3637static final double infinityD = Double.POSITIVE_INFINITY;38static final double NaNd = Double.NaN;3940/**41* Formulation taken from HP-15C Advanced Functions Handbook, part42* number HP 0015-90011, p 181. This is accurate to a few ulps.43*/44static double hp15cLogp(double x) {45double u = 1.0 + x;46return (u==1.0? x : StrictMath.log(u)*x/(u-1) );47}4849/*50* The Taylor expansion of ln(1 + x) for -1 < x <= 1 is:51*52* x - x^2/2 + x^3/3 - ... -(-x^j)/j53*54* Therefore, for small values of x, log1p(x) ~= x. For large55* values of x, log1p(x) ~= log(x).56*57* Also x/(x+1) < ln(1+x) < x58*/5960static int testLog1p() {61int failures = 0;6263double [][] testCases = {64{Double.NaN, NaNd},65{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},66{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},67{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},68{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},69{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},70{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},71{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},72{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},73{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},74{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},75{Double.NEGATIVE_INFINITY, NaNd},76{-8.0, NaNd},77{-1.0, -infinityD},78{-0.0, -0.0},79{+0.0, +0.0},80{infinityD, infinityD},81};8283// Test special cases84for(int i = 0; i < testCases.length; i++) {85failures += testLog1pCaseWithUlpDiff(testCases[i][0],86testCases[i][1], 0);87}8889// For |x| < 2^-54 log1p(x) ~= x90for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {91double d = Math.scalb(2, i);92failures += testLog1pCase(d, d);93failures += testLog1pCase(-d, -d);94}9596// For x > 2^53 log1p(x) ~= log(x)97for(int i = 53; i <= DoubleConsts.MAX_EXPONENT; i++) {98double d = Math.scalb(2, i);99failures += testLog1pCaseWithUlpDiff(d, StrictMath.log(d), 2.001);100}101102// Construct random values with exponents ranging from -53 to103// 52 and compare against HP-15C formula.104java.util.Random rand = new java.util.Random();105for(int i = 0; i < 1000; i++) {106double d = rand.nextDouble();107108d = Math.scalb(d, -53 - FpUtils.ilogb(d));109110for(int j = -53; j <= 52; j++) {111failures += testLog1pCaseWithUlpDiff(d, hp15cLogp(d), 5);112113d *= 2.0; // increase exponent by 1114}115}116117// Test for monotonicity failures near values y-1 where y ~=118// e^x. Test two numbers before and two numbers after each119// chosen value; i.e.120//121// pcNeighbors[] =122// {nextDown(nextDown(pc)),123// nextDown(pc),124// pc,125// nextUp(pc),126// nextUp(nextUp(pc))}127//128// and we test that log1p(pcNeighbors[i]) <= log1p(pcNeighbors[i+1])129{130double pcNeighbors[] = new double[5];131double pcNeighborsLog1p[] = new double[5];132double pcNeighborsStrictLog1p[] = new double[5];133134for(int i = -36; i <= 36; i++) {135double pc = StrictMath.pow(Math.E, i) - 1;136137pcNeighbors[2] = pc;138pcNeighbors[1] = Math.nextDown(pc);139pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);140pcNeighbors[3] = Math.nextUp(pc);141pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);142143for(int j = 0; j < pcNeighbors.length; j++) {144pcNeighborsLog1p[j] = Math.log1p(pcNeighbors[j]);145pcNeighborsStrictLog1p[j] = StrictMath.log1p(pcNeighbors[j]);146}147148for(int j = 0; j < pcNeighborsLog1p.length-1; j++) {149if(pcNeighborsLog1p[j] > pcNeighborsLog1p[j+1] ) {150failures++;151System.err.println("Monotonicity failure for Math.log1p on " +152pcNeighbors[j] + " and " +153pcNeighbors[j+1] + "\n\treturned " +154pcNeighborsLog1p[j] + " and " +155pcNeighborsLog1p[j+1] );156}157158if(pcNeighborsStrictLog1p[j] > pcNeighborsStrictLog1p[j+1] ) {159failures++;160System.err.println("Monotonicity failure for StrictMath.log1p on " +161pcNeighbors[j] + " and " +162pcNeighbors[j+1] + "\n\treturned " +163pcNeighborsStrictLog1p[j] + " and " +164pcNeighborsStrictLog1p[j+1] );165}166167168}169170}171}172173return failures;174}175176public static int testLog1pCase(double input,177double expected) {178return testLog1pCaseWithUlpDiff(input, expected, 1);179}180181public static int testLog1pCaseWithUlpDiff(double input,182double expected,183double ulps) {184int failures = 0;185failures += Tests.testUlpDiff("Math.lop1p(double",186input, Math.log1p(input),187expected, ulps);188failures += Tests.testUlpDiff("StrictMath.log1p(double",189input, StrictMath.log1p(input),190expected, ulps);191return failures;192}193194public static void main(String argv[]) {195int failures = 0;196197failures += testLog1p();198199if (failures > 0) {200System.err.println("Testing log1p incurred "201+ failures + " failures.");202throw new RuntimeException();203}204}205}206207208