test/java/lang/Math/Expm1Tests.java

Print this page


   1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   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 4900189 4939441
  27  * @summary Tests for {Math, StrictMath}.expm1
  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import sun.misc.DoubleConsts;
  32 import sun.misc.FpUtils;
  33 
  34 /*
  35  * The Taylor expansion of expxm1(x) = exp(x) -1 is
  36  *
  37  * 1 + x/1! + x^2/2! + x^3/3| + ... -1 =
  38  *
  39  * x + x^2/2! + x^3/3 + ...
  40  *
  41  * Therefore, for small values of x, expxm1 ~= x.
  42  *
  43  * For large values of x, expxm1(x) ~= exp(x)
  44  *
  45  * For large negative x, expxm1(x) ~= -1.
  46  */
  47 
  48 public class Expm1Tests {
  49 
  50     private Expm1Tests(){}
  51 
  52     static final double infinityD = Double.POSITIVE_INFINITY;


 126         // Test two numbers before and two numbers after each chosen
 127         // value; i.e.
 128         //
 129         // pcNeighbors[] =
 130         // {nextDown(nextDown(pc)),
 131         // nextDown(pc),
 132         // pc,
 133         // nextUp(pc),
 134         // nextUp(nextUp(pc))}
 135         //
 136         // and we test that expm1(pcNeighbors[i]) <= expm1(pcNeighbors[i+1])
 137         {
 138             double pcNeighbors[] = new double[5];
 139             double pcNeighborsExpm1[] = new double[5];
 140             double pcNeighborsStrictExpm1[] = new double[5];
 141 
 142             for(int i = -50; i <= 50; i++) {
 143                 double pc = StrictMath.log(2)*i;
 144 
 145                 pcNeighbors[2] = pc;
 146                 pcNeighbors[1] = FpUtils.nextDown(pc);
 147                 pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
 148                 pcNeighbors[3] = Math.nextUp(pc);
 149                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
 150 
 151                 for(int j = 0; j < pcNeighbors.length; j++) {
 152                     pcNeighborsExpm1[j]       =       Math.expm1(pcNeighbors[j]);
 153                     pcNeighborsStrictExpm1[j] = StrictMath.expm1(pcNeighbors[j]);
 154                 }
 155 
 156                 for(int j = 0; j < pcNeighborsExpm1.length-1; j++) {
 157                     if(pcNeighborsExpm1[j] >  pcNeighborsExpm1[j+1] ) {
 158                         failures++;
 159                         System.err.println("Monotonicity failure for Math.expm1 on " +
 160                                           pcNeighbors[j] + " and "  +
 161                                           pcNeighbors[j+1] + "\n\treturned " +
 162                                           pcNeighborsExpm1[j] + " and " +
 163                                           pcNeighborsExpm1[j+1] );
 164                     }
 165 
 166                     if(pcNeighborsStrictExpm1[j] >  pcNeighborsStrictExpm1[j+1] ) {
 167                         failures++;


   1 /*
   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.
   4  *
   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
   7  * published by the Free Software Foundation.
   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 4900189 4939441
  27  * @summary Tests for {Math, StrictMath}.expm1
  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import sun.misc.DoubleConsts;

  32 
  33 /*
  34  * The Taylor expansion of expxm1(x) = exp(x) -1 is
  35  *
  36  * 1 + x/1! + x^2/2! + x^3/3| + ... -1 =
  37  *
  38  * x + x^2/2! + x^3/3 + ...
  39  *
  40  * Therefore, for small values of x, expxm1 ~= x.
  41  *
  42  * For large values of x, expxm1(x) ~= exp(x)
  43  *
  44  * For large negative x, expxm1(x) ~= -1.
  45  */
  46 
  47 public class Expm1Tests {
  48 
  49     private Expm1Tests(){}
  50 
  51     static final double infinityD = Double.POSITIVE_INFINITY;


 125         // Test two numbers before and two numbers after each chosen
 126         // value; i.e.
 127         //
 128         // pcNeighbors[] =
 129         // {nextDown(nextDown(pc)),
 130         // nextDown(pc),
 131         // pc,
 132         // nextUp(pc),
 133         // nextUp(nextUp(pc))}
 134         //
 135         // and we test that expm1(pcNeighbors[i]) <= expm1(pcNeighbors[i+1])
 136         {
 137             double pcNeighbors[] = new double[5];
 138             double pcNeighborsExpm1[] = new double[5];
 139             double pcNeighborsStrictExpm1[] = new double[5];
 140 
 141             for(int i = -50; i <= 50; i++) {
 142                 double pc = StrictMath.log(2)*i;
 143 
 144                 pcNeighbors[2] = pc;
 145                 pcNeighbors[1] = Math.nextDown(pc);
 146                 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);
 147                 pcNeighbors[3] = Math.nextUp(pc);
 148                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
 149 
 150                 for(int j = 0; j < pcNeighbors.length; j++) {
 151                     pcNeighborsExpm1[j]       =       Math.expm1(pcNeighbors[j]);
 152                     pcNeighborsStrictExpm1[j] = StrictMath.expm1(pcNeighbors[j]);
 153                 }
 154 
 155                 for(int j = 0; j < pcNeighborsExpm1.length-1; j++) {
 156                     if(pcNeighborsExpm1[j] >  pcNeighborsExpm1[j+1] ) {
 157                         failures++;
 158                         System.err.println("Monotonicity failure for Math.expm1 on " +
 159                                           pcNeighbors[j] + " and "  +
 160                                           pcNeighbors[j+1] + "\n\treturned " +
 161                                           pcNeighborsExpm1[j] + " and " +
 162                                           pcNeighborsExpm1[j+1] );
 163                     }
 164 
 165                     if(pcNeighborsStrictExpm1[j] >  pcNeighborsStrictExpm1[j+1] ) {
 166                         failures++;