Path: blob/master/test/jdk/java/lang/Math/Log10Tests.java
41152 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*/2930public class Log10Tests {31private Log10Tests(){}3233static final double infinityD = Double.POSITIVE_INFINITY;34static final double NaNd = Double.NaN;35static final double LN_10 = StrictMath.log(10.0);3637// Initialize shared random number generator38static java.util.Random rand = new java.util.Random(0L);3940static int testLog10Case(double input, double expected) {41int failures=0;4243failures+=Tests.test("Math.log10(double)", input,44Math.log10(input), expected);4546failures+=Tests.test("StrictMath.log10(double)", input,47StrictMath.log10(input), expected);4849return failures;50}5152static int testLog10() {53int failures = 0;5455double [][] testCases = {56{Double.NaN, NaNd},57{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},58{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},59{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},60{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},61{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},62{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},63{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},64{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},65{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},66{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},67{Double.NEGATIVE_INFINITY, NaNd},68{-8.0, NaNd},69{-1.0, NaNd},70{-Double.MIN_NORMAL, NaNd},71{-Double.MIN_VALUE, NaNd},72{-0.0, -infinityD},73{+0.0, -infinityD},74{+1.0, 0.0},75{Double.POSITIVE_INFINITY, infinityD},76};7778// Test special cases79for(int i = 0; i < testCases.length; i++) {80failures += testLog10Case(testCases[i][0],81testCases[i][1]);82}8384// Test log10(10^n) == n for integer n; 10^n, n < 0 is not85// exactly representable as a floating-point value -- up to86// 10^22 can be represented exactly87double testCase = 1.0;88for(int i = 0; i < 23; i++) {89failures += testLog10Case(testCase, i);90testCase *= 10.0;91}9293// Test for gross inaccuracy by comparing to log; should be94// within a few ulps of log(x)/log(10)95for(int i = 0; i < 10000; i++) {96double input = Double.longBitsToDouble(rand.nextLong());97if(! Double.isFinite(input))98continue; // avoid testing NaN and infinite values99else {100input = Math.abs(input);101102double expected = StrictMath.log(input)/LN_10;103if( ! Double.isFinite(expected))104continue; // if log(input) overflowed, try again105else {106double result;107108if( Math.abs(((result=Math.log10(input)) - expected)/Math.ulp(expected)) > 3) {109failures++;110System.err.println("For input " + input +111", Math.log10 was more than 3 ulps different from " +112"log(input)/log(10): log10(input) = " + result +113"\tlog(input)/log(10) = " + expected);114}115116if( Math.abs(((result=StrictMath.log10(input)) - expected)/Math.ulp(expected)) > 3) {117failures++;118System.err.println("For input " + input +119", StrictMath.log10 was more than 3 ulps different from " +120"log(input)/log(10): log10(input) = " + result +121"\tlog(input)/log(10) = " + expected);122}123124125}126}127}128129// Test for accuracy and monotonicity near log10(1.0). From130// the Taylor expansion of log,131// log10(1+z) ~= (z -(z^2)/2)/LN_10;132{133double neighbors[] = new double[40];134double neighborsStrict[] = new double[40];135double z = Double.NaN;136137// Test inputs greater than 1.0.138neighbors[0] = Math.log10(1.0);139neighborsStrict[0] = StrictMath.log10(1.0);140141double input[] = new double[40];142int half = input.length/2;143144145// Initialize input to the 40 consecutive double values146// "centered" at 1.0.147double up = Double.NaN;148double down = Double.NaN;149for(int i = 0; i < half; i++) {150if (i == 0) {151input[half] = 1.0;152up = Math.nextUp(1.0);153down = Math.nextDown(1.0);154} else {155input[half + i] = up;156input[half - i] = down;157up = Math.nextUp(up);158down = Math.nextDown(down);159}160}161input[0] = Math.nextDown(input[1]);162163for(int i = 0; i < neighbors.length; i++) {164neighbors[i] = Math.log10(input[i]);165neighborsStrict[i] = StrictMath.log10(input[i]);166167// Test accuracy.168z = input[i] - 1.0;169double expected = (z - (z*z)*0.5)/LN_10;170if ( Math.abs(neighbors[i] - expected ) > 3*Math.ulp(expected) ) {171failures++;172System.err.println("For input near 1.0 " + input[i] +173", Math.log10(1+z) was more than 3 ulps different from " +174"(z-(z^2)/2)/ln(10): log10(input) = " + neighbors[i] +175"\texpected about = " + expected);176}177178if ( Math.abs(neighborsStrict[i] - expected ) > 3*Math.ulp(expected) ) {179failures++;180System.err.println("For input near 1.0 " + input[i] +181", StrictMath.log10(1+z) was more than 3 ulps different from " +182"(z-(z^2)/2)/ln(10): log10(input) = " + neighborsStrict[i] +183"\texpected about = " + expected);184}185186// Test monotonicity187if( i > 0) {188if( neighbors[i-1] > neighbors[i] ) {189failures++;190System.err.println("Monotonicity failure for Math.log10 at " + input[i] +191" and prior value.");192}193194if( neighborsStrict[i-1] > neighborsStrict[i] ) {195failures++;196System.err.println("Monotonicity failure for StrictMath.log10 at " + input[i] +197" and prior value.");198}199}200}201202}203204return failures;205}206207public static void main(String argv[]) {208int failures = 0;209210failures += testLog10();211212if (failures > 0) {213System.err.println("Testing log10 incurred "214+ failures + " failures.");215throw new RuntimeException();216}217}218219}220221222