test/java/lang/Math/Log1pTests.java

Print this page




  71             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
  72             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
  73             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
  74             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
  75             {Double.NEGATIVE_INFINITY,  NaNd},
  76             {-8.0,                      NaNd},
  77             {-1.0,                      -infinityD},
  78             {-0.0,                      -0.0},
  79             {+0.0,                      +0.0},
  80             {infinityD,                 infinityD},
  81         };
  82 
  83         // Test special cases
  84         for(int i = 0; i < testCases.length; i++) {
  85             failures += testLog1pCaseWithUlpDiff(testCases[i][0],
  86                                                  testCases[i][1], 0);
  87         }
  88 
  89         // For |x| < 2^-54 log1p(x) ~= x
  90         for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
  91             double d = FpUtils.scalb(2, i);
  92             failures += testLog1pCase(d, d);
  93             failures += testLog1pCase(-d, -d);
  94         }
  95 
  96         // For x > 2^53 log1p(x) ~= log(x)
  97         for(int i = 53; i <= DoubleConsts.MAX_EXPONENT; i++) {
  98             double d = FpUtils.scalb(2, i);
  99             failures += testLog1pCaseWithUlpDiff(d, StrictMath.log(d), 2.001);
 100         }
 101 
 102         // Construct random values with exponents ranging from -53 to
 103         // 52 and compare against HP-15C formula.
 104         java.util.Random rand = new java.util.Random();
 105         for(int i = 0; i < 1000; i++) {
 106             double d = rand.nextDouble();
 107 
 108             d = FpUtils.scalb(d, -53 - FpUtils.ilogb(d));
 109 
 110             for(int j = -53; j <= 52; j++) {
 111                 failures += testLog1pCaseWithUlpDiff(d, hp15cLogp(d), 5);
 112 
 113                 d *= 2.0; // increase exponent by 1
 114             }
 115         }
 116 
 117         // Test for monotonicity failures near values y-1 where y ~=
 118         // e^x.  Test two numbers before and two numbers after each
 119         // chosen value; i.e.
 120         //
 121         // pcNeighbors[] =
 122         // {nextDown(nextDown(pc)),
 123         // nextDown(pc),
 124         // pc,
 125         // nextUp(pc),
 126         // nextUp(nextUp(pc))}
 127         //
 128         // and we test that log1p(pcNeighbors[i]) <= log1p(pcNeighbors[i+1])
 129         {
 130             double pcNeighbors[] = new double[5];
 131             double pcNeighborsLog1p[] = new double[5];
 132             double pcNeighborsStrictLog1p[] = new double[5];
 133 
 134             for(int i = -36; i <= 36; i++) {
 135                 double pc = StrictMath.pow(Math.E, i) - 1;
 136 
 137                 pcNeighbors[2] = pc;
 138                 pcNeighbors[1] = FpUtils.nextDown(pc);
 139                 pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
 140                 pcNeighbors[3] = FpUtils.nextUp(pc);
 141                 pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]);
 142 
 143                 for(int j = 0; j < pcNeighbors.length; j++) {
 144                     pcNeighborsLog1p[j]       =       Math.log1p(pcNeighbors[j]);
 145                     pcNeighborsStrictLog1p[j] = StrictMath.log1p(pcNeighbors[j]);
 146                 }
 147 
 148                 for(int j = 0; j < pcNeighborsLog1p.length-1; j++) {
 149                     if(pcNeighborsLog1p[j] >  pcNeighborsLog1p[j+1] ) {
 150                         failures++;
 151                         System.err.println("Monotonicity failure for Math.log1p on " +
 152                                           pcNeighbors[j] + " and "  +
 153                                           pcNeighbors[j+1] + "\n\treturned " +
 154                                           pcNeighborsLog1p[j] + " and " +
 155                                           pcNeighborsLog1p[j+1] );
 156                     }
 157 
 158                     if(pcNeighborsStrictLog1p[j] >  pcNeighborsStrictLog1p[j+1] ) {
 159                         failures++;
 160                         System.err.println("Monotonicity failure for StrictMath.log1p on " +
 161                                           pcNeighbors[j] + " and "  +




  71             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
  72             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
  73             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
  74             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
  75             {Double.NEGATIVE_INFINITY,  NaNd},
  76             {-8.0,                      NaNd},
  77             {-1.0,                      -infinityD},
  78             {-0.0,                      -0.0},
  79             {+0.0,                      +0.0},
  80             {infinityD,                 infinityD},
  81         };
  82 
  83         // Test special cases
  84         for(int i = 0; i < testCases.length; i++) {
  85             failures += testLog1pCaseWithUlpDiff(testCases[i][0],
  86                                                  testCases[i][1], 0);
  87         }
  88 
  89         // For |x| < 2^-54 log1p(x) ~= x
  90         for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
  91             double d = Math.scalb(2, i);
  92             failures += testLog1pCase(d, d);
  93             failures += testLog1pCase(-d, -d);
  94         }
  95 
  96         // For x > 2^53 log1p(x) ~= log(x)
  97         for(int i = 53; i <= DoubleConsts.MAX_EXPONENT; i++) {
  98             double d = Math.scalb(2, i);
  99             failures += testLog1pCaseWithUlpDiff(d, StrictMath.log(d), 2.001);
 100         }
 101 
 102         // Construct random values with exponents ranging from -53 to
 103         // 52 and compare against HP-15C formula.
 104         java.util.Random rand = new java.util.Random();
 105         for(int i = 0; i < 1000; i++) {
 106             double d = rand.nextDouble();
 107 
 108             d = Math.scalb(d, -53 - FpUtils.ilogb(d));
 109 
 110             for(int j = -53; j <= 52; j++) {
 111                 failures += testLog1pCaseWithUlpDiff(d, hp15cLogp(d), 5);
 112 
 113                 d *= 2.0; // increase exponent by 1
 114             }
 115         }
 116 
 117         // Test for monotonicity failures near values y-1 where y ~=
 118         // e^x.  Test two numbers before and two numbers after each
 119         // chosen value; i.e.
 120         //
 121         // pcNeighbors[] =
 122         // {nextDown(nextDown(pc)),
 123         // nextDown(pc),
 124         // pc,
 125         // nextUp(pc),
 126         // nextUp(nextUp(pc))}
 127         //
 128         // and we test that log1p(pcNeighbors[i]) <= log1p(pcNeighbors[i+1])
 129         {
 130             double pcNeighbors[] = new double[5];
 131             double pcNeighborsLog1p[] = new double[5];
 132             double pcNeighborsStrictLog1p[] = new double[5];
 133 
 134             for(int i = -36; i <= 36; i++) {
 135                 double pc = StrictMath.pow(Math.E, i) - 1;
 136 
 137                 pcNeighbors[2] = pc;
 138                 pcNeighbors[1] = FpUtils.nextDown(pc);
 139                 pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
 140                 pcNeighbors[3] = Math.nextUp(pc);
 141                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
 142 
 143                 for(int j = 0; j < pcNeighbors.length; j++) {
 144                     pcNeighborsLog1p[j]       =       Math.log1p(pcNeighbors[j]);
 145                     pcNeighborsStrictLog1p[j] = StrictMath.log1p(pcNeighbors[j]);
 146                 }
 147 
 148                 for(int j = 0; j < pcNeighborsLog1p.length-1; j++) {
 149                     if(pcNeighborsLog1p[j] >  pcNeighborsLog1p[j+1] ) {
 150                         failures++;
 151                         System.err.println("Monotonicity failure for Math.log1p on " +
 152                                           pcNeighbors[j] + " and "  +
 153                                           pcNeighbors[j+1] + "\n\treturned " +
 154                                           pcNeighborsLog1p[j] + " and " +
 155                                           pcNeighborsLog1p[j+1] );
 156                     }
 157 
 158                     if(pcNeighborsStrictLog1p[j] >  pcNeighborsStrictLog1p[j+1] ) {
 159                         failures++;
 160                         System.err.println("Monotonicity failure for StrictMath.log1p on " +
 161                                           pcNeighbors[j] + " and "  +