test/java/lang/Math/Log1pTests.java

Print this page




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4851638 4939441
  27  * @summary Tests for {Math, StrictMath}.log1p



  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import sun.misc.DoubleConsts;
  32 
  33 public class Log1pTests {
  34     private Log1pTests(){}
  35 
  36     static final double infinityD = Double.POSITIVE_INFINITY;
  37     static final double NaNd = Double.NaN;
  38 
  39     /**
  40      * Formulation taken from HP-15C Advanced Functions Handbook, part
  41      * number HP 0015-90011, p 181.  This is accurate to a few ulps.
  42      */
  43     static double hp15cLogp(double x) {
  44         double u = 1.0 + x;
  45         return (u==1.0? x : StrictMath.log(u)*x/(u-1) );
  46     }
  47 
  48     /*
  49      * The Taylor expansion of ln(1 + x) for -1 < x <= 1 is:
  50      *
  51      * x - x^2/2 + x^3/3 - ... -(-x^j)/j


  69             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
  70             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
  71             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
  72             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
  73             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
  74             {Double.NEGATIVE_INFINITY,  NaNd},
  75             {-8.0,                      NaNd},
  76             {-1.0,                      -infinityD},
  77             {-0.0,                      -0.0},
  78             {+0.0,                      +0.0},
  79             {infinityD,                 infinityD},
  80         };
  81 
  82         // Test special cases
  83         for(int i = 0; i < testCases.length; i++) {
  84             failures += testLog1pCaseWithUlpDiff(testCases[i][0],
  85                                                  testCases[i][1], 0);
  86         }
  87 
  88         // For |x| < 2^-54 log1p(x) ~= x
  89         for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
  90             double d = Math.scalb(2, i);
  91             failures += testLog1pCase(d, d);
  92             failures += testLog1pCase(-d, -d);
  93         }
  94 
  95         // For x > 2^53 log1p(x) ~= log(x)
  96         for(int i = 53; i <= DoubleConsts.MAX_EXPONENT; i++) {
  97             double d = Math.scalb(2, i);
  98             failures += testLog1pCaseWithUlpDiff(d, StrictMath.log(d), 2.001);
  99         }
 100 
 101         // Construct random values with exponents ranging from -53 to
 102         // 52 and compare against HP-15C formula.
 103         java.util.Random rand = new java.util.Random();
 104         for(int i = 0; i < 1000; i++) {
 105             double d = rand.nextDouble();
 106 
 107             d = Math.scalb(d, -53 - Tests.ilogb(d));
 108 
 109             for(int j = -53; j <= 52; j++) {
 110                 failures += testLog1pCaseWithUlpDiff(d, hp15cLogp(d), 5);
 111 
 112                 d *= 2.0; // increase exponent by 1
 113             }
 114         }
 115 
 116         // Test for monotonicity failures near values y-1 where y ~=




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4851638 4939441
  27  * @summary Tests for {Math, StrictMath}.log1p
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.DoubleUtils jdk.testlibrary.FloatUtils
  30  * @run main Log1pTests
  31  * @author Joseph D. Darcy
  32  */
  33 
  34 import static jdk.testlibrary.DoubleUtils.*;
  35 
  36 public class Log1pTests {
  37     private Log1pTests(){}
  38 
  39     static final double infinityD = Double.POSITIVE_INFINITY;
  40     static final double NaNd = Double.NaN;
  41 
  42     /**
  43      * Formulation taken from HP-15C Advanced Functions Handbook, part
  44      * number HP 0015-90011, p 181.  This is accurate to a few ulps.
  45      */
  46     static double hp15cLogp(double x) {
  47         double u = 1.0 + x;
  48         return (u==1.0? x : StrictMath.log(u)*x/(u-1) );
  49     }
  50 
  51     /*
  52      * The Taylor expansion of ln(1 + x) for -1 < x <= 1 is:
  53      *
  54      * x - x^2/2 + x^3/3 - ... -(-x^j)/j


  72             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
  73             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
  74             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
  75             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
  76             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
  77             {Double.NEGATIVE_INFINITY,  NaNd},
  78             {-8.0,                      NaNd},
  79             {-1.0,                      -infinityD},
  80             {-0.0,                      -0.0},
  81             {+0.0,                      +0.0},
  82             {infinityD,                 infinityD},
  83         };
  84 
  85         // Test special cases
  86         for(int i = 0; i < testCases.length; i++) {
  87             failures += testLog1pCaseWithUlpDiff(testCases[i][0],
  88                                                  testCases[i][1], 0);
  89         }
  90 
  91         // For |x| < 2^-54 log1p(x) ~= x
  92         for(int i = MIN_SUB_EXPONENT; i <= -54; i++) {
  93             double d = Math.scalb(2, i);
  94             failures += testLog1pCase(d, d);
  95             failures += testLog1pCase(-d, -d);
  96         }
  97 
  98         // For x > 2^53 log1p(x) ~= log(x)
  99         for(int i = 53; i <= Double.MAX_EXPONENT; i++) {
 100             double d = Math.scalb(2, i);
 101             failures += testLog1pCaseWithUlpDiff(d, StrictMath.log(d), 2.001);
 102         }
 103 
 104         // Construct random values with exponents ranging from -53 to
 105         // 52 and compare against HP-15C formula.
 106         java.util.Random rand = new java.util.Random();
 107         for(int i = 0; i < 1000; i++) {
 108             double d = rand.nextDouble();
 109 
 110             d = Math.scalb(d, -53 - Tests.ilogb(d));
 111 
 112             for(int j = -53; j <= 52; j++) {
 113                 failures += testLog1pCaseWithUlpDiff(d, hp15cLogp(d), 5);
 114 
 115                 d *= 2.0; // increase exponent by 1
 116             }
 117         }
 118 
 119         // Test for monotonicity failures near values y-1 where y ~=