1 /*
2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
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 4851638 4939441
27 * @summary Tests for {Math, StrictMath}.hypot
28 * @author Joseph D. Darcy
29 * @key randomness
30 */
31
32 public class HypotTests {
33 private HypotTests(){}
34
35 static final double infinityD = Double.POSITIVE_INFINITY;
36 static final double NaNd = Double.NaN;
37
38 /**
39 * Given integers m and n, assuming m < n, the triple (n^2 - m^2,
40 * 2mn, and n^2 + m^2) is a Pythagorean triple with a^2 + b^2 =
41 * c^2. This methods returns a long array holding the Pythagorean
42 * triple corresponding to the inputs.
43 */
44 static long [] pythagoreanTriple(int m, int n) {
45 long M = m;
46 long N = n;
47 long result[] = new long[3];
103 }
104 }
105
106 // Big ones
107 for(int m = 100000; m < 100100; m++) {
108 for(int n = m+100000; n < 200200; n++) {
109 long [] result = pythagoreanTriple(m, n);
110 failures += testHypotCase(result[0], result[1], result[2]);
111 }
112 }
113
114 // Approaching overflow tests
115
116 /*
117 * Create a random value r with an large-ish exponent. The
118 * result of hypot(3*r, 4*r) should be approximately 5*r. (The
119 * computation of 4*r is exact since it just changes the
120 * exponent). While the exponent of r is less than or equal
121 * to (MAX_EXPONENT - 3), the computation should not overflow.
122 */
123 java.util.Random rand = new java.util.Random();
124 for(int i = 0; i < 1000; i++) {
125 double d = rand.nextDouble();
126 // Scale d to have an exponent equal to MAX_EXPONENT -15
127 d = Math.scalb(d, Double.MAX_EXPONENT
128 -15 - Tests.ilogb(d));
129 for(int j = 0; j <= 13; j += 1) {
130 failures += testHypotCase(3*d, 4*d, 5*d, 2.5);
131 d *= 2.0; // increase exponent by 1
132 }
133 }
134
135 // Test for monotonicity failures. Fix one argument and test
136 // two numbers before and two numbers after each chosen value;
137 // i.e.
138 //
139 // pcNeighbors[] =
140 // {nextDown(nextDown(pc)),
141 // nextDown(pc),
142 // pc,
143 // nextUp(pc),
|
1 /*
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
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 * @library /lib/testlibrary/
27 * @build jdk.testlibrary.*
28 * @run main HypotTests
29 * @bug 4851638 4939441 8078672
30 * @summary Tests for {Math, StrictMath}.hypot (use -Dseed=X to set PRNG seed)
31 * @author Joseph D. Darcy
32 * @key randomness
33 */
34
35 public class HypotTests {
36 private HypotTests(){}
37
38 static final double infinityD = Double.POSITIVE_INFINITY;
39 static final double NaNd = Double.NaN;
40
41 /**
42 * Given integers m and n, assuming m < n, the triple (n^2 - m^2,
43 * 2mn, and n^2 + m^2) is a Pythagorean triple with a^2 + b^2 =
44 * c^2. This methods returns a long array holding the Pythagorean
45 * triple corresponding to the inputs.
46 */
47 static long [] pythagoreanTriple(int m, int n) {
48 long M = m;
49 long N = n;
50 long result[] = new long[3];
106 }
107 }
108
109 // Big ones
110 for(int m = 100000; m < 100100; m++) {
111 for(int n = m+100000; n < 200200; n++) {
112 long [] result = pythagoreanTriple(m, n);
113 failures += testHypotCase(result[0], result[1], result[2]);
114 }
115 }
116
117 // Approaching overflow tests
118
119 /*
120 * Create a random value r with an large-ish exponent. The
121 * result of hypot(3*r, 4*r) should be approximately 5*r. (The
122 * computation of 4*r is exact since it just changes the
123 * exponent). While the exponent of r is less than or equal
124 * to (MAX_EXPONENT - 3), the computation should not overflow.
125 */
126 java.util.Random rand = RandomFactory.getRandom();
127 for(int i = 0; i < 1000; i++) {
128 double d = rand.nextDouble();
129 // Scale d to have an exponent equal to MAX_EXPONENT -15
130 d = Math.scalb(d, Double.MAX_EXPONENT
131 -15 - Tests.ilogb(d));
132 for(int j = 0; j <= 13; j += 1) {
133 failures += testHypotCase(3*d, 4*d, 5*d, 2.5);
134 d *= 2.0; // increase exponent by 1
135 }
136 }
137
138 // Test for monotonicity failures. Fix one argument and test
139 // two numbers before and two numbers after each chosen value;
140 // i.e.
141 //
142 // pcNeighbors[] =
143 // {nextDown(nextDown(pc)),
144 // nextDown(pc),
145 // pc,
146 // nextUp(pc),
|