1 /*
   2  * Copyright (c) 2003, 2012, 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;
  52     static final double NaNd = Double.NaN;
  53 
  54     static int testExpm1() {
  55         int failures = 0;
  56 
  57         double [][] 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             {infinityD,                 infinityD},
  70             {-infinityD,                -1.0},
  71             {-0.0,                      -0.0},
  72             {+0.0,                      +0.0},
  73         };
  74 
  75         // Test special cases
  76         for(int i = 0; i < testCases.length; i++) {
  77             failures += testExpm1CaseWithUlpDiff(testCases[i][0],
  78                                                  testCases[i][1], 0, null);
  79         }
  80 
  81 
  82         // For |x| < 2^-54 expm1(x) ~= x
  83         for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
  84             double d = Math.scalb(2, i);
  85             failures += testExpm1Case(d, d);
  86             failures += testExpm1Case(-d, -d);
  87         }
  88 
  89 
  90         // For values of y where exp(y) > 2^54, expm1(x) ~= exp(x).
  91         // The least such y is ln(2^54) ~= 37.42994775023705; exp(x)
  92         // overflows for x > ~= 709.8
  93 
  94         // Use a 2-ulp error threshold to account for errors in the
  95         // exp implementation; the increments of d in the loop will be
  96         // exact.
  97         for(double d = 37.5; d <= 709.5; d += 1.0) {
  98             failures += testExpm1CaseWithUlpDiff(d, StrictMath.exp(d), 2, null);
  99         }
 100 
 101         // For x > 710, expm1(x) should be infinity
 102         for(int i = 10; i <= DoubleConsts.MAX_EXPONENT; i++) {
 103             double d = Math.scalb(2, i);
 104             failures += testExpm1Case(d, infinityD);
 105         }
 106 
 107         // By monotonicity, once the limit is reached, the
 108         // implemenation should return the limit for all smaller
 109         // values.
 110         boolean reachedLimit [] = {false, false};
 111 
 112         // Once exp(y) < 0.5 * ulp(1), expm1(y) ~= -1.0;
 113         // The greatest such y is ln(2^-53) ~= -36.7368005696771.
 114         for(double d = -36.75; d >= -127.75; d -= 1.0) {
 115             failures += testExpm1CaseWithUlpDiff(d, -1.0, 1,
 116                                                  reachedLimit);
 117         }
 118 
 119         for(int i = 7; i <= DoubleConsts.MAX_EXPONENT; i++) {
 120             double d = -Math.scalb(2, i);
 121             failures += testExpm1CaseWithUlpDiff(d, -1.0, 1, reachedLimit);
 122         }
 123 
 124         // Test for monotonicity failures near multiples of log(2).
 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++;
 167                         System.err.println("Monotonicity failure for StrictMath.expm1 on " +
 168                                           pcNeighbors[j] + " and "  +
 169                                           pcNeighbors[j+1] + "\n\treturned " +
 170                                           pcNeighborsStrictExpm1[j] + " and " +
 171                                           pcNeighborsStrictExpm1[j+1] );
 172                     }
 173 
 174 
 175                 }
 176 
 177             }
 178         }
 179 
 180         return failures;
 181     }
 182 
 183     public static int testExpm1Case(double input,
 184                                     double expected) {
 185         return testExpm1CaseWithUlpDiff(input, expected, 1, null);
 186     }
 187 
 188     public static int testExpm1CaseWithUlpDiff(double input,
 189                                                double expected,
 190                                                double ulps,
 191                                                boolean [] reachedLimit) {
 192         int failures = 0;
 193         double mathUlps = ulps, strictUlps = ulps;
 194         double mathOutput;
 195         double strictOutput;
 196 
 197         if (reachedLimit != null) {
 198             if (reachedLimit[0])
 199                 mathUlps = 0;
 200 
 201             if (reachedLimit[1])
 202                 strictUlps = 0;
 203         }
 204 
 205         failures += Tests.testUlpDiffWithLowerBound("Math.expm1(double)",
 206                                                     input, mathOutput=Math.expm1(input),
 207                                                     expected, mathUlps, -1.0);
 208         failures += Tests.testUlpDiffWithLowerBound("StrictMath.expm1(double)",
 209                                                     input, strictOutput=StrictMath.expm1(input),
 210                                                     expected, strictUlps, -1.0);
 211         if (reachedLimit != null) {
 212             reachedLimit[0] |= (mathOutput   == -1.0);
 213             reachedLimit[1] |= (strictOutput == -1.0);
 214         }
 215 
 216         return failures;
 217     }
 218 
 219     public static void main(String argv[]) {
 220         int failures = 0;
 221 
 222         failures += testExpm1();
 223 
 224         if (failures > 0) {
 225             System.err.println("Testing expm1 incurred "
 226                                + failures + " failures.");
 227             throw new RuntimeException();
 228         }
 229     }
 230 }