test/java/lang/Math/HyperbolicTests.java

Print this page




  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 4851625 4900189 4939441
  27  * @summary Tests for {Math, StrictMath}.{sinh, cosh, tanh}
  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import sun.misc.DoubleConsts;
  32 
  33 public class HyperbolicTests {
  34     private HyperbolicTests(){}
  35 
  36     static final double NaNd = Double.NaN;
  37 
  38     /**
  39      * Test accuracy of {Math, StrictMath}.sinh.  The specified
  40      * accuracy is 2.5 ulps.
  41      *
  42      * The defintion of sinh(x) is
  43      *
  44      * (e^x - e^(-x))/2
  45      *
  46      * The series expansion of sinh(x) =
  47      *
  48      * x + x^3/3! + x^5/5! + x^7/7! +...
  49      *
  50      * Therefore,
  51      *
  52      * 1. For large values of x sinh(x) ~= signum(x)*exp(|x|)/2


 247             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      NaNd},
 248             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
 249             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
 250             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
 251             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
 252             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
 253             {Double.POSITIVE_INFINITY,  Double.POSITIVE_INFINITY}
 254         };
 255 
 256         for(int i = 0; i < specialTestCases.length; i++) {
 257             failures += testSinhCaseWithUlpDiff(specialTestCases[i][0],
 258                                                 specialTestCases[i][1],
 259                                                 0.0);
 260         }
 261 
 262         // For powers of 2 less than 2^(-27), the second and
 263         // subsequent terms of the Taylor series expansion will get
 264         // rounded away since |n-n^3| > 53, the binary precision of a
 265         // double significand.
 266 
 267         for(int i = DoubleConsts.MIN_SUB_EXPONENT; i < -27; i++) {
 268             double d = Math.scalb(2.0, i);
 269 
 270             // Result and expected are the same.
 271             failures += testSinhCaseWithUlpDiff(d, d, 2.5);
 272         }
 273 
 274         // For values of x larger than 22, the e^(-x) term is
 275         // insignificant to the floating-point result.  Util exp(x)
 276         // overflows around 709.8, sinh(x) ~= exp(x)/2; will will test
 277         // 10000 values in this range.
 278 
 279         long trans22 = Double.doubleToLongBits(22.0);
 280         // (approximately) largest value such that exp shouldn't
 281         // overflow
 282         long transExpOvfl = Double.doubleToLongBits(Math.nextDown(709.7827128933841));
 283 
 284         for(long i = trans22;
 285             i < transExpOvfl;
 286             i +=(transExpOvfl-trans22)/10000) {
 287 


 325          * e^(x + offset)
 326          *
 327          * than exp(x+offset) alone.  (The expected result cannot be
 328          * computed as exp(x)*exp(offset) since exp(x) by itself would
 329          * overflow to infinity.)
 330          */
 331         double offset = StrictMath.log(0.5);
 332         for(long i = transExpOvfl+1; i < transSinhOvfl;
 333             i += (transSinhOvfl-transExpOvfl)/1000 ) {
 334             double input = Double.longBitsToDouble(i);
 335 
 336             double expected =
 337                 StrictMath.exp(input + offset) *
 338                 StrictMath.exp( offset - ((input + offset) - input) );
 339 
 340             failures += testSinhCaseWithUlpDiff(input, expected, 4.0);
 341         }
 342 
 343         // sinh(x) overflows for values greater than 710; in
 344         // particular, it overflows for all 2^i, i > 10.
 345         for(int i = 10; i <= DoubleConsts.MAX_EXPONENT; i++) {
 346             double d = Math.scalb(2.0, i);
 347 
 348             // Result and expected are the same.
 349             failures += testSinhCaseWithUlpDiff(d,
 350                                                 Double.POSITIVE_INFINITY, 0.0);
 351         }
 352 
 353         return failures;
 354     }
 355 
 356     public static int testSinhCaseWithTolerance(double input,
 357                                                 double expected,
 358                                                 double tolerance) {
 359         int failures = 0;
 360         failures += Tests.testTolerance("Math.sinh(double)",
 361                                         input, Math.sinh(input),
 362                                         expected, tolerance);
 363         failures += Tests.testTolerance("Math.sinh(double)",
 364                                         -input, Math.sinh(-input),
 365                                         -expected, tolerance);


 606             {Double.longBitsToDouble(0xFFF8555555555555L),      NaNd},
 607             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      NaNd},
 608             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
 609             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
 610             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
 611             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
 612             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
 613             {Double.POSITIVE_INFINITY,  Double.POSITIVE_INFINITY}
 614         };
 615 
 616         for(int i = 0; i < specialTestCases.length; i++ ) {
 617             failures += testCoshCaseWithUlpDiff(specialTestCases[i][0],
 618                                                 specialTestCases[i][1],
 619                                                 0.0);
 620         }
 621 
 622         // For powers of 2 less than 2^(-27), the second and
 623         // subsequent terms of the Taylor series expansion will get
 624         // rounded.
 625 
 626         for(int i = DoubleConsts.MIN_SUB_EXPONENT; i < -27; i++) {
 627             double d = Math.scalb(2.0, i);
 628 
 629             // Result and expected are the same.
 630             failures += testCoshCaseWithUlpDiff(d, 1.0, 2.5);
 631         }
 632 
 633         // For values of x larger than 22, the e^(-x) term is
 634         // insignificant to the floating-point result.  Util exp(x)
 635         // overflows around 709.8, cosh(x) ~= exp(x)/2; will will test
 636         // 10000 values in this range.
 637 
 638         long trans22 = Double.doubleToLongBits(22.0);
 639         // (approximately) largest value such that exp shouldn't
 640         // overflow
 641         long transExpOvfl = Double.doubleToLongBits(Math.nextDown(709.7827128933841));
 642 
 643         for(long i = trans22;
 644             i < transExpOvfl;
 645             i +=(transExpOvfl-trans22)/10000) {
 646 


 684          * e^(x + offset)
 685          *
 686          * than exp(x+offset) alone.  (The expected result cannot be
 687          * computed as exp(x)*exp(offset) since exp(x) by itself would
 688          * overflow to infinity.)
 689          */
 690         double offset = StrictMath.log(0.5);
 691         for(long i = transExpOvfl+1; i < transCoshOvfl;
 692             i += (transCoshOvfl-transExpOvfl)/1000 ) {
 693             double input = Double.longBitsToDouble(i);
 694 
 695             double expected =
 696                 StrictMath.exp(input + offset) *
 697                 StrictMath.exp( offset - ((input + offset) - input) );
 698 
 699             failures += testCoshCaseWithUlpDiff(input, expected, 4.0);
 700         }
 701 
 702         // cosh(x) overflows for values greater than 710; in
 703         // particular, it overflows for all 2^i, i > 10.
 704         for(int i = 10; i <= DoubleConsts.MAX_EXPONENT; i++) {
 705             double d = Math.scalb(2.0, i);
 706 
 707             // Result and expected are the same.
 708             failures += testCoshCaseWithUlpDiff(d,
 709                                                 Double.POSITIVE_INFINITY, 0.0);
 710         }
 711         return failures;
 712     }
 713 
 714     public static int testCoshCaseWithTolerance(double input,
 715                                                 double expected,
 716                                                 double tolerance) {
 717         int failures = 0;
 718         failures += Tests.testTolerance("Math.cosh(double)",
 719                                         input, Math.cosh(input),
 720                                         expected, tolerance);
 721         failures += Tests.testTolerance("Math.cosh(double)",
 722                                         -input, Math.cosh(-input),
 723                                         expected, tolerance);
 724 


 965             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      NaNd},
 966             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
 967             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
 968             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
 969             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
 970             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
 971             {Double.POSITIVE_INFINITY,  1.0}
 972         };
 973 
 974         for(int i = 0; i < specialTestCases.length; i++) {
 975             failures += testTanhCaseWithUlpDiff(specialTestCases[i][0],
 976                                                 specialTestCases[i][1],
 977                                                 0.0);
 978         }
 979 
 980         // For powers of 2 less than 2^(-27), the second and
 981         // subsequent terms of the Taylor series expansion will get
 982         // rounded away since |n-n^3| > 53, the binary precision of a
 983         // double significand.
 984 
 985         for(int i = DoubleConsts.MIN_SUB_EXPONENT; i < -27; i++) {
 986             double d = Math.scalb(2.0, i);
 987 
 988             // Result and expected are the same.
 989             failures += testTanhCaseWithUlpDiff(d, d, 2.5);
 990         }
 991 
 992         // For values of x larger than 22, tanh(x) is 1.0 in double
 993         // floating-point arithmetic.
 994 
 995         for(int i = 22; i < 32; i++) {
 996             failures += testTanhCaseWithUlpDiff(i, 1.0, 2.5);
 997         }
 998 
 999         for(int i = 5; i <= DoubleConsts.MAX_EXPONENT; i++) {
1000             double d = Math.scalb(2.0, i);
1001 
1002             failures += testTanhCaseWithUlpDiff(d, 1.0, 2.5);
1003         }
1004 
1005         return failures;
1006     }
1007 
1008     public static int testTanhCaseWithTolerance(double input,
1009                                                 double expected,
1010                                                 double tolerance) {
1011         int failures = 0;
1012         failures += Tests.testTolerance("Math.tanh(double",
1013                                         input, Math.tanh(input),
1014                                         expected, tolerance);
1015         failures += Tests.testTolerance("Math.tanh(double",
1016                                         -input, Math.tanh(-input),
1017                                         -expected, tolerance);
1018 
1019         failures += Tests.testTolerance("StrictMath.tanh(double",




  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 4851625 4900189 4939441
  27  * @summary Tests for {Math, StrictMath}.{sinh, cosh, tanh}
  28  * @author Joseph D. Darcy
  29  */
  30 


  31 public class HyperbolicTests {
  32     private HyperbolicTests(){}
  33 
  34     static final double NaNd = Double.NaN;
  35 
  36     /**
  37      * Test accuracy of {Math, StrictMath}.sinh.  The specified
  38      * accuracy is 2.5 ulps.
  39      *
  40      * The defintion of sinh(x) is
  41      *
  42      * (e^x - e^(-x))/2
  43      *
  44      * The series expansion of sinh(x) =
  45      *
  46      * x + x^3/3! + x^5/5! + x^7/7! +...
  47      *
  48      * Therefore,
  49      *
  50      * 1. For large values of x sinh(x) ~= signum(x)*exp(|x|)/2


 245             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      NaNd},
 246             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
 247             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
 248             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
 249             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
 250             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
 251             {Double.POSITIVE_INFINITY,  Double.POSITIVE_INFINITY}
 252         };
 253 
 254         for(int i = 0; i < specialTestCases.length; i++) {
 255             failures += testSinhCaseWithUlpDiff(specialTestCases[i][0],
 256                                                 specialTestCases[i][1],
 257                                                 0.0);
 258         }
 259 
 260         // For powers of 2 less than 2^(-27), the second and
 261         // subsequent terms of the Taylor series expansion will get
 262         // rounded away since |n-n^3| > 53, the binary precision of a
 263         // double significand.
 264 
 265         for(int i = DoubleUtils.MIN_SUB_EXPONENT; i < -27; i++) {
 266             double d = Math.scalb(2.0, i);
 267 
 268             // Result and expected are the same.
 269             failures += testSinhCaseWithUlpDiff(d, d, 2.5);
 270         }
 271 
 272         // For values of x larger than 22, the e^(-x) term is
 273         // insignificant to the floating-point result.  Util exp(x)
 274         // overflows around 709.8, sinh(x) ~= exp(x)/2; will will test
 275         // 10000 values in this range.
 276 
 277         long trans22 = Double.doubleToLongBits(22.0);
 278         // (approximately) largest value such that exp shouldn't
 279         // overflow
 280         long transExpOvfl = Double.doubleToLongBits(Math.nextDown(709.7827128933841));
 281 
 282         for(long i = trans22;
 283             i < transExpOvfl;
 284             i +=(transExpOvfl-trans22)/10000) {
 285 


 323          * e^(x + offset)
 324          *
 325          * than exp(x+offset) alone.  (The expected result cannot be
 326          * computed as exp(x)*exp(offset) since exp(x) by itself would
 327          * overflow to infinity.)
 328          */
 329         double offset = StrictMath.log(0.5);
 330         for(long i = transExpOvfl+1; i < transSinhOvfl;
 331             i += (transSinhOvfl-transExpOvfl)/1000 ) {
 332             double input = Double.longBitsToDouble(i);
 333 
 334             double expected =
 335                 StrictMath.exp(input + offset) *
 336                 StrictMath.exp( offset - ((input + offset) - input) );
 337 
 338             failures += testSinhCaseWithUlpDiff(input, expected, 4.0);
 339         }
 340 
 341         // sinh(x) overflows for values greater than 710; in
 342         // particular, it overflows for all 2^i, i > 10.
 343         for(int i = 10; i <= Double.MAX_EXPONENT; i++) {
 344             double d = Math.scalb(2.0, i);
 345 
 346             // Result and expected are the same.
 347             failures += testSinhCaseWithUlpDiff(d,
 348                                                 Double.POSITIVE_INFINITY, 0.0);
 349         }
 350 
 351         return failures;
 352     }
 353 
 354     public static int testSinhCaseWithTolerance(double input,
 355                                                 double expected,
 356                                                 double tolerance) {
 357         int failures = 0;
 358         failures += Tests.testTolerance("Math.sinh(double)",
 359                                         input, Math.sinh(input),
 360                                         expected, tolerance);
 361         failures += Tests.testTolerance("Math.sinh(double)",
 362                                         -input, Math.sinh(-input),
 363                                         -expected, tolerance);


 604             {Double.longBitsToDouble(0xFFF8555555555555L),      NaNd},
 605             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      NaNd},
 606             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
 607             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
 608             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
 609             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
 610             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
 611             {Double.POSITIVE_INFINITY,  Double.POSITIVE_INFINITY}
 612         };
 613 
 614         for(int i = 0; i < specialTestCases.length; i++ ) {
 615             failures += testCoshCaseWithUlpDiff(specialTestCases[i][0],
 616                                                 specialTestCases[i][1],
 617                                                 0.0);
 618         }
 619 
 620         // For powers of 2 less than 2^(-27), the second and
 621         // subsequent terms of the Taylor series expansion will get
 622         // rounded.
 623 
 624         for(int i = DoubleUtils.MIN_SUB_EXPONENT; i < -27; i++) {
 625             double d = Math.scalb(2.0, i);
 626 
 627             // Result and expected are the same.
 628             failures += testCoshCaseWithUlpDiff(d, 1.0, 2.5);
 629         }
 630 
 631         // For values of x larger than 22, the e^(-x) term is
 632         // insignificant to the floating-point result.  Util exp(x)
 633         // overflows around 709.8, cosh(x) ~= exp(x)/2; will will test
 634         // 10000 values in this range.
 635 
 636         long trans22 = Double.doubleToLongBits(22.0);
 637         // (approximately) largest value such that exp shouldn't
 638         // overflow
 639         long transExpOvfl = Double.doubleToLongBits(Math.nextDown(709.7827128933841));
 640 
 641         for(long i = trans22;
 642             i < transExpOvfl;
 643             i +=(transExpOvfl-trans22)/10000) {
 644 


 682          * e^(x + offset)
 683          *
 684          * than exp(x+offset) alone.  (The expected result cannot be
 685          * computed as exp(x)*exp(offset) since exp(x) by itself would
 686          * overflow to infinity.)
 687          */
 688         double offset = StrictMath.log(0.5);
 689         for(long i = transExpOvfl+1; i < transCoshOvfl;
 690             i += (transCoshOvfl-transExpOvfl)/1000 ) {
 691             double input = Double.longBitsToDouble(i);
 692 
 693             double expected =
 694                 StrictMath.exp(input + offset) *
 695                 StrictMath.exp( offset - ((input + offset) - input) );
 696 
 697             failures += testCoshCaseWithUlpDiff(input, expected, 4.0);
 698         }
 699 
 700         // cosh(x) overflows for values greater than 710; in
 701         // particular, it overflows for all 2^i, i > 10.
 702         for(int i = 10; i <= Double.MAX_EXPONENT; i++) {
 703             double d = Math.scalb(2.0, i);
 704 
 705             // Result and expected are the same.
 706             failures += testCoshCaseWithUlpDiff(d,
 707                                                 Double.POSITIVE_INFINITY, 0.0);
 708         }
 709         return failures;
 710     }
 711 
 712     public static int testCoshCaseWithTolerance(double input,
 713                                                 double expected,
 714                                                 double tolerance) {
 715         int failures = 0;
 716         failures += Tests.testTolerance("Math.cosh(double)",
 717                                         input, Math.cosh(input),
 718                                         expected, tolerance);
 719         failures += Tests.testTolerance("Math.cosh(double)",
 720                                         -input, Math.cosh(-input),
 721                                         expected, tolerance);
 722 


 963             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      NaNd},
 964             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
 965             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
 966             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
 967             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
 968             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
 969             {Double.POSITIVE_INFINITY,  1.0}
 970         };
 971 
 972         for(int i = 0; i < specialTestCases.length; i++) {
 973             failures += testTanhCaseWithUlpDiff(specialTestCases[i][0],
 974                                                 specialTestCases[i][1],
 975                                                 0.0);
 976         }
 977 
 978         // For powers of 2 less than 2^(-27), the second and
 979         // subsequent terms of the Taylor series expansion will get
 980         // rounded away since |n-n^3| > 53, the binary precision of a
 981         // double significand.
 982 
 983         for(int i = DoubleUtils.MIN_SUB_EXPONENT; i < -27; i++) {
 984             double d = Math.scalb(2.0, i);
 985 
 986             // Result and expected are the same.
 987             failures += testTanhCaseWithUlpDiff(d, d, 2.5);
 988         }
 989 
 990         // For values of x larger than 22, tanh(x) is 1.0 in double
 991         // floating-point arithmetic.
 992 
 993         for(int i = 22; i < 32; i++) {
 994             failures += testTanhCaseWithUlpDiff(i, 1.0, 2.5);
 995         }
 996 
 997         for(int i = 5; i <= Double.MAX_EXPONENT; i++) {
 998             double d = Math.scalb(2.0, i);
 999 
1000             failures += testTanhCaseWithUlpDiff(d, 1.0, 2.5);
1001         }
1002 
1003         return failures;
1004     }
1005 
1006     public static int testTanhCaseWithTolerance(double input,
1007                                                 double expected,
1008                                                 double tolerance) {
1009         int failures = 0;
1010         failures += Tests.testTolerance("Math.tanh(double",
1011                                         input, Math.tanh(input),
1012                                         expected, tolerance);
1013         failures += Tests.testTolerance("Math.tanh(double",
1014                                         -input, Math.tanh(-input),
1015                                         -expected, tolerance);
1016 
1017         failures += Tests.testTolerance("StrictMath.tanh(double",