test/java/lang/Math/HyperbolicTests.java

Print this page




   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 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      *


 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",




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


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


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


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


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


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