Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Math/Log10Tests.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 4074599 493944126* @summary Tests for {Math, StrictMath}.log1027* @author Joseph D. Darcy28*/2930import sun.misc.DoubleConsts;3132public class Log10Tests {33private Log10Tests(){}3435static final double infinityD = Double.POSITIVE_INFINITY;36static final double NaNd = Double.NaN;37static final double LN_10 = StrictMath.log(10.0);3839// Initialize shared random number generator40static java.util.Random rand = new java.util.Random(0L);4142static int testLog10Case(double input, double expected) {43int failures=0;4445failures+=Tests.test("Math.log10(double)", input,46Math.log10(input), expected);4748failures+=Tests.test("StrictMath.log10(double)", input,49StrictMath.log10(input), expected);5051return failures;52}5354static int testLog10() {55int failures = 0;5657double [][] testCases = {58{Double.NaN, NaNd},59{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},60{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},61{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},62{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},63{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},64{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},65{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},66{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},67{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},68{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},69{Double.NEGATIVE_INFINITY, NaNd},70{-8.0, NaNd},71{-1.0, NaNd},72{-DoubleConsts.MIN_NORMAL, NaNd},73{-Double.MIN_VALUE, NaNd},74{-0.0, -infinityD},75{+0.0, -infinityD},76{+1.0, 0.0},77{Double.POSITIVE_INFINITY, infinityD},78};7980// Test special cases81for(int i = 0; i < testCases.length; i++) {82failures += testLog10Case(testCases[i][0],83testCases[i][1]);84}8586// Test log10(10^n) == n for integer n; 10^n, n < 0 is not87// exactly representable as a floating-point value -- up to88// 10^22 can be represented exactly89double testCase = 1.0;90for(int i = 0; i < 23; i++) {91failures += testLog10Case(testCase, i);92testCase *= 10.0;93}9495// Test for gross inaccuracy by comparing to log; should be96// within a few ulps of log(x)/log(10)97for(int i = 0; i < 10000; i++) {98double input = Double.longBitsToDouble(rand.nextLong());99if(! Double.isFinite(input))100continue; // avoid testing NaN and infinite values101else {102input = Math.abs(input);103104double expected = StrictMath.log(input)/LN_10;105if( ! Double.isFinite(expected))106continue; // if log(input) overflowed, try again107else {108double result;109110if( Math.abs(((result=Math.log10(input)) - expected)/Math.ulp(expected)) > 3) {111failures++;112System.err.println("For input " + input +113", Math.log10 was more than 3 ulps different from " +114"log(input)/log(10): log10(input) = " + result +115"\tlog(input)/log(10) = " + expected);116}117118if( Math.abs(((result=StrictMath.log10(input)) - expected)/Math.ulp(expected)) > 3) {119failures++;120System.err.println("For input " + input +121", StrictMath.log10 was more than 3 ulps different from " +122"log(input)/log(10): log10(input) = " + result +123"\tlog(input)/log(10) = " + expected);124}125126127}128}129}130131// Test for accuracy and monotonicity near log10(1.0). From132// the Taylor expansion of log,133// log10(1+z) ~= (z -(z^2)/2)/LN_10;134{135double neighbors[] = new double[40];136double neighborsStrict[] = new double[40];137double z = Double.NaN;138139// Test inputs greater than 1.0.140neighbors[0] = Math.log10(1.0);141neighborsStrict[0] = StrictMath.log10(1.0);142143double input[] = new double[40];144int half = input.length/2;145146147// Initialize input to the 40 consecutive double values148// "centered" at 1.0.149double up = Double.NaN;150double down = Double.NaN;151for(int i = 0; i < half; i++) {152if (i == 0) {153input[half] = 1.0;154up = Math.nextUp(1.0);155down = Math.nextDown(1.0);156} else {157input[half + i] = up;158input[half - i] = down;159up = Math.nextUp(up);160down = Math.nextDown(down);161}162}163input[0] = Math.nextDown(input[1]);164165for(int i = 0; i < neighbors.length; i++) {166neighbors[i] = Math.log10(input[i]);167neighborsStrict[i] = StrictMath.log10(input[i]);168169// Test accuracy.170z = input[i] - 1.0;171double expected = (z - (z*z)*0.5)/LN_10;172if ( Math.abs(neighbors[i] - expected ) > 3*Math.ulp(expected) ) {173failures++;174System.err.println("For input near 1.0 " + input[i] +175", Math.log10(1+z) was more than 3 ulps different from " +176"(z-(z^2)/2)/ln(10): log10(input) = " + neighbors[i] +177"\texpected about = " + expected);178}179180if ( Math.abs(neighborsStrict[i] - expected ) > 3*Math.ulp(expected) ) {181failures++;182System.err.println("For input near 1.0 " + input[i] +183", StrictMath.log10(1+z) was more than 3 ulps different from " +184"(z-(z^2)/2)/ln(10): log10(input) = " + neighborsStrict[i] +185"\texpected about = " + expected);186}187188// Test monotonicity189if( i > 0) {190if( neighbors[i-1] > neighbors[i] ) {191failures++;192System.err.println("Monotonicity failure for Math.log10 at " + input[i] +193" and prior value.");194}195196if( neighborsStrict[i-1] > neighborsStrict[i] ) {197failures++;198System.err.println("Monotonicity failure for StrictMath.log10 at " + input[i] +199" and prior value.");200}201}202}203204}205206return failures;207}208209public static void main(String argv[]) {210int failures = 0;211212failures += testLog10();213214if (failures > 0) {215System.err.println("Testing log10 incurred "216+ failures + " failures.");217throw new RuntimeException();218}219}220221}222223224