test/java/lang/Math/Expm1Tests.java

Print this page




  65             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
  66             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
  67             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
  68             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
  69             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
  70             {infinityD,                 infinityD},
  71             {-infinityD,                -1.0},
  72             {-0.0,                      -0.0},
  73             {+0.0,                      +0.0},
  74         };
  75 
  76         // Test special cases
  77         for(int i = 0; i < testCases.length; i++) {
  78             failures += testExpm1CaseWithUlpDiff(testCases[i][0],
  79                                                  testCases[i][1], 0, null);
  80         }
  81 
  82 
  83         // For |x| < 2^-54 expm1(x) ~= x
  84         for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
  85             double d = FpUtils.scalb(2, i);
  86             failures += testExpm1Case(d, d);
  87             failures += testExpm1Case(-d, -d);
  88         }
  89 
  90 
  91         // For values of y where exp(y) > 2^54, expm1(x) ~= exp(x).
  92         // The least such y is ln(2^54) ~= 37.42994775023705; exp(x)
  93         // overflows for x > ~= 709.8
  94 
  95         // Use a 2-ulp error threshold to account for errors in the
  96         // exp implementation; the increments of d in the loop will be
  97         // exact.
  98         for(double d = 37.5; d <= 709.5; d += 1.0) {
  99             failures += testExpm1CaseWithUlpDiff(d, StrictMath.exp(d), 2, null);
 100         }
 101 
 102         // For x > 710, expm1(x) should be infinity
 103         for(int i = 10; i <= DoubleConsts.MAX_EXPONENT; i++) {
 104             double d = FpUtils.scalb(2, i);
 105             failures += testExpm1Case(d, infinityD);
 106         }
 107 
 108         // By monotonicity, once the limit is reached, the
 109         // implemenation should return the limit for all smaller
 110         // values.
 111         boolean reachedLimit [] = {false, false};
 112 
 113         // Once exp(y) < 0.5 * ulp(1), expm1(y) ~= -1.0;
 114         // The greatest such y is ln(2^-53) ~= -36.7368005696771.
 115         for(double d = -36.75; d >= -127.75; d -= 1.0) {
 116             failures += testExpm1CaseWithUlpDiff(d, -1.0, 1,
 117                                                  reachedLimit);
 118         }
 119 
 120         for(int i = 7; i <= DoubleConsts.MAX_EXPONENT; i++) {
 121             double d = -FpUtils.scalb(2, i);
 122             failures += testExpm1CaseWithUlpDiff(d, -1.0, 1, reachedLimit);
 123         }
 124 
 125         // Test for monotonicity failures near multiples of log(2).
 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] = FpUtils.nextUp(pc);
 149                 pcNeighbors[4] = FpUtils.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++;
 168                         System.err.println("Monotonicity failure for StrictMath.expm1 on " +
 169                                           pcNeighbors[j] + " and "  +




  65             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
  66             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
  67             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
  68             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
  69             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
  70             {infinityD,                 infinityD},
  71             {-infinityD,                -1.0},
  72             {-0.0,                      -0.0},
  73             {+0.0,                      +0.0},
  74         };
  75 
  76         // Test special cases
  77         for(int i = 0; i < testCases.length; i++) {
  78             failures += testExpm1CaseWithUlpDiff(testCases[i][0],
  79                                                  testCases[i][1], 0, null);
  80         }
  81 
  82 
  83         // For |x| < 2^-54 expm1(x) ~= x
  84         for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
  85             double d = Math.scalb(2, i);
  86             failures += testExpm1Case(d, d);
  87             failures += testExpm1Case(-d, -d);
  88         }
  89 
  90 
  91         // For values of y where exp(y) > 2^54, expm1(x) ~= exp(x).
  92         // The least such y is ln(2^54) ~= 37.42994775023705; exp(x)
  93         // overflows for x > ~= 709.8
  94 
  95         // Use a 2-ulp error threshold to account for errors in the
  96         // exp implementation; the increments of d in the loop will be
  97         // exact.
  98         for(double d = 37.5; d <= 709.5; d += 1.0) {
  99             failures += testExpm1CaseWithUlpDiff(d, StrictMath.exp(d), 2, null);
 100         }
 101 
 102         // For x > 710, expm1(x) should be infinity
 103         for(int i = 10; i <= DoubleConsts.MAX_EXPONENT; i++) {
 104             double d = Math.scalb(2, i);
 105             failures += testExpm1Case(d, infinityD);
 106         }
 107 
 108         // By monotonicity, once the limit is reached, the
 109         // implemenation should return the limit for all smaller
 110         // values.
 111         boolean reachedLimit [] = {false, false};
 112 
 113         // Once exp(y) < 0.5 * ulp(1), expm1(y) ~= -1.0;
 114         // The greatest such y is ln(2^-53) ~= -36.7368005696771.
 115         for(double d = -36.75; d >= -127.75; d -= 1.0) {
 116             failures += testExpm1CaseWithUlpDiff(d, -1.0, 1,
 117                                                  reachedLimit);
 118         }
 119 
 120         for(int i = 7; i <= DoubleConsts.MAX_EXPONENT; i++) {
 121             double d = -Math.scalb(2, i);
 122             failures += testExpm1CaseWithUlpDiff(d, -1.0, 1, reachedLimit);
 123         }
 124 
 125         // Test for monotonicity failures near multiples of log(2).
 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++;
 168                         System.err.println("Monotonicity failure for StrictMath.expm1 on " +
 169                                           pcNeighbors[j] + " and "  +