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
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);
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
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
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);
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
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 = DoubleConsts.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",
|