jdk/test/java/lang/Math/Log10Tests.java
changeset 10608 7cfca36fc79b
parent 10598 efd29b4b3e67
child 14856 92a1bcf46888
equal deleted inserted replaced
10607:1ba66093449c 10608:7cfca36fc79b
     1 /*
     1 /*
     2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2011 Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    26  * @bug 4074599 4939441
    26  * @bug 4074599 4939441
    27  * @summary Tests for {Math, StrictMath}.log10
    27  * @summary Tests for {Math, StrictMath}.log10
    28  * @author Joseph D. Darcy
    28  * @author Joseph D. Darcy
    29  */
    29  */
    30 
    30 
    31 import sun.misc.FpUtils;
       
    32 import sun.misc.DoubleConsts;
    31 import sun.misc.DoubleConsts;
    33 
    32 
    34 public class Log10Tests {
    33 public class Log10Tests {
    35     private Log10Tests(){}
    34     private Log10Tests(){}
    36 
    35 
    96 
    95 
    97         // Test for gross inaccuracy by comparing to log; should be
    96         // Test for gross inaccuracy by comparing to log; should be
    98         // within a few ulps of log(x)/log(10)
    97         // within a few ulps of log(x)/log(10)
    99         for(int i = 0; i < 10000; i++) {
    98         for(int i = 0; i < 10000; i++) {
   100             double input = Double.longBitsToDouble(rand.nextLong());
    99             double input = Double.longBitsToDouble(rand.nextLong());
   101             if(! FpUtils.isFinite(input))
   100             if(! Double.isFinite(input))
   102                 continue; // avoid testing NaN and infinite values
   101                 continue; // avoid testing NaN and infinite values
   103             else {
   102             else {
   104                 input = Math.abs(input);
   103                 input = Math.abs(input);
   105 
   104 
   106                 double expected = StrictMath.log(input)/LN_10;
   105                 double expected = StrictMath.log(input)/LN_10;
   107                 if( ! FpUtils.isFinite(expected))
   106                 if( ! Double.isFinite(expected))
   108                     continue; // if log(input) overflowed, try again
   107                     continue; // if log(input) overflowed, try again
   109                 else {
   108                 else {
   110                     double result;
   109                     double result;
   111 
   110 
   112                     if( Math.abs(((result=Math.log10(input)) - expected)/Math.ulp(expected)) > 3) {
   111                     if( Math.abs(((result=Math.log10(input)) - expected)/Math.ulp(expected)) > 3) {
   152             double down = Double.NaN;
   151             double down = Double.NaN;
   153             for(int i = 0; i < half; i++) {
   152             for(int i = 0; i < half; i++) {
   154                 if (i == 0) {
   153                 if (i == 0) {
   155                     input[half] = 1.0;
   154                     input[half] = 1.0;
   156                     up   = Math.nextUp(1.0);
   155                     up   = Math.nextUp(1.0);
   157                     down = FpUtils.nextDown(1.0);
   156                     down = Math.nextDown(1.0);
   158                 } else {
   157                 } else {
   159                     input[half + i] = up;
   158                     input[half + i] = up;
   160                     input[half - i] = down;
   159                     input[half - i] = down;
   161                     up   = Math.nextUp(up);
   160                     up   = Math.nextUp(up);
   162                     down = FpUtils.nextDown(down);
   161                     down = Math.nextDown(down);
   163                 }
   162                 }
   164             }
   163             }
   165             input[0] = FpUtils.nextDown(input[1]);
   164             input[0] = Math.nextDown(input[1]);
   166 
   165 
   167             for(int i = 0; i < neighbors.length; i++) {
   166             for(int i = 0; i < neighbors.length; i++) {
   168                 neighbors[i] =          Math.log10(input[i]);
   167                 neighbors[i] =          Math.log10(input[i]);
   169                 neighborsStrict[i] =    StrictMath.log10(input[i]);
   168                 neighborsStrict[i] =    StrictMath.log10(input[i]);
   170 
   169