test/java/lang/Math/HypotTests.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 4851638 4939441
  27  * @summary Tests for {Math, StrictMath}.hypot
  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import sun.misc.DoubleConsts;
  32 
  33 public class HypotTests {
  34     private HypotTests(){}
  35 
  36     static final double infinityD = Double.POSITIVE_INFINITY;
  37     static final double NaNd      = Double.NaN;
  38 
  39     /**
  40      * Given integers m and n, assuming m < n, the triple (n^2 - m^2,
  41      * 2mn, and n^2 + m^2) is a Pythagorean triple with a^2 + b^2 =
  42      * c^2.  This methods returns a long array holding the Pythagorean
  43      * triple corresponding to the inputs.
  44      */
  45     static long [] pythagoreanTriple(int m, int n) {
  46         long M = m;
  47         long N = n;
  48         long result[] = new long[3];
  49 
  50 
  51         result[0] = Math.abs(M*M - N*N);
  52         result[1] = Math.abs(2*M*N);


  69             {1.0,               NaNd,                   NaNd},
  70             {Double.longBitsToDouble(0x7FF0000000000001L),      1.0,    NaNd},
  71             {Double.longBitsToDouble(0xFFF0000000000001L),      1.0,    NaNd},
  72             {Double.longBitsToDouble(0x7FF8555555555555L),      1.0,    NaNd},
  73             {Double.longBitsToDouble(0xFFF8555555555555L),      1.0,    NaNd},
  74             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      1.0,    NaNd},
  75             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      1.0,    NaNd},
  76             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      1.0,    NaNd},
  77             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      1.0,    NaNd},
  78             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      1.0,    NaNd},
  79             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      1.0,    NaNd},
  80         };
  81 
  82         for(int i = 0; i < testCases.length; i++) {
  83             failures += testHypotCase(testCases[i][0], testCases[i][1],
  84                                       testCases[i][2]);
  85         }
  86 
  87         // Verify hypot(x, 0.0) is close to x over the entire exponent
  88         // range.
  89         for(int i = DoubleConsts.MIN_SUB_EXPONENT;
  90             i <= DoubleConsts.MAX_EXPONENT;
  91             i++) {
  92             double input = Math.scalb(2, i);
  93             failures += testHypotCase(input, 0.0, input);
  94         }
  95 
  96 
  97         // Test Pythagorean triples
  98 
  99         // Small ones
 100         for(int m = 1; m < 10; m++) {
 101             for(int n = m+1; n < 11; n++) {
 102                 long [] result = pythagoreanTriple(m, n);
 103                 failures += testHypotCase(result[0], result[1], result[2]);
 104             }
 105         }
 106 
 107         // Big ones
 108         for(int m = 100000; m < 100100; m++) {
 109             for(int n = m+100000; n < 200200; n++) {
 110                 long [] result = pythagoreanTriple(m, n);
 111                 failures += testHypotCase(result[0], result[1], result[2]);
 112             }
 113         }
 114 
 115         // Approaching overflow tests
 116 
 117         /*
 118          * Create a random value r with an large-ish exponent.  The
 119          * result of hypot(3*r, 4*r) should be approximately 5*r. (The
 120          * computation of 4*r is exact since it just changes the
 121          * exponent).  While the exponent of r is less than or equal
 122          * to (MAX_EXPONENT - 3), the computation should not overflow.
 123          */
 124         java.util.Random rand = new java.util.Random();
 125         for(int i = 0; i < 1000; i++) {
 126             double d = rand.nextDouble();
 127             // Scale d to have an exponent equal to MAX_EXPONENT -15
 128             d = Math.scalb(d, DoubleConsts.MAX_EXPONENT
 129                                  -15 - Tests.ilogb(d));
 130             for(int j = 0; j <= 13; j += 1) {
 131                 failures += testHypotCase(3*d, 4*d, 5*d, 2.5);
 132                 d *= 2.0; // increase exponent by 1
 133             }
 134         }
 135 
 136         // Test for monotonicity failures.  Fix one argument and test
 137         // two numbers before and two numbers after each chosen value;
 138         // i.e.
 139         //
 140         // pcNeighbors[] =
 141         // {nextDown(nextDown(pc)),
 142         // nextDown(pc),
 143         // pc,
 144         // nextUp(pc),
 145         // nextUp(nextUp(pc))}
 146         //
 147         // and we test that hypot(pcNeighbors[i]) <= hypot(pcNeighbors[i+1])
 148         {




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


  31 public class HypotTests {
  32     private HypotTests(){}
  33 
  34     static final double infinityD = Double.POSITIVE_INFINITY;
  35     static final double NaNd      = Double.NaN;
  36 
  37     /**
  38      * Given integers m and n, assuming m < n, the triple (n^2 - m^2,
  39      * 2mn, and n^2 + m^2) is a Pythagorean triple with a^2 + b^2 =
  40      * c^2.  This methods returns a long array holding the Pythagorean
  41      * triple corresponding to the inputs.
  42      */
  43     static long [] pythagoreanTriple(int m, int n) {
  44         long M = m;
  45         long N = n;
  46         long result[] = new long[3];
  47 
  48 
  49         result[0] = Math.abs(M*M - N*N);
  50         result[1] = Math.abs(2*M*N);


  67             {1.0,               NaNd,                   NaNd},
  68             {Double.longBitsToDouble(0x7FF0000000000001L),      1.0,    NaNd},
  69             {Double.longBitsToDouble(0xFFF0000000000001L),      1.0,    NaNd},
  70             {Double.longBitsToDouble(0x7FF8555555555555L),      1.0,    NaNd},
  71             {Double.longBitsToDouble(0xFFF8555555555555L),      1.0,    NaNd},
  72             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      1.0,    NaNd},
  73             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      1.0,    NaNd},
  74             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      1.0,    NaNd},
  75             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      1.0,    NaNd},
  76             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      1.0,    NaNd},
  77             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      1.0,    NaNd},
  78         };
  79 
  80         for(int i = 0; i < testCases.length; i++) {
  81             failures += testHypotCase(testCases[i][0], testCases[i][1],
  82                                       testCases[i][2]);
  83         }
  84 
  85         // Verify hypot(x, 0.0) is close to x over the entire exponent
  86         // range.
  87         for(int i = DoubleUtils.MIN_SUB_EXPONENT;
  88             i <= Double.MAX_EXPONENT;
  89             i++) {
  90             double input = Math.scalb(2, i);
  91             failures += testHypotCase(input, 0.0, input);
  92         }
  93 
  94 
  95         // Test Pythagorean triples
  96 
  97         // Small ones
  98         for(int m = 1; m < 10; m++) {
  99             for(int n = m+1; n < 11; n++) {
 100                 long [] result = pythagoreanTriple(m, n);
 101                 failures += testHypotCase(result[0], result[1], result[2]);
 102             }
 103         }
 104 
 105         // Big ones
 106         for(int m = 100000; m < 100100; m++) {
 107             for(int n = m+100000; n < 200200; n++) {
 108                 long [] result = pythagoreanTriple(m, n);
 109                 failures += testHypotCase(result[0], result[1], result[2]);
 110             }
 111         }
 112 
 113         // Approaching overflow tests
 114 
 115         /*
 116          * Create a random value r with an large-ish exponent.  The
 117          * result of hypot(3*r, 4*r) should be approximately 5*r. (The
 118          * computation of 4*r is exact since it just changes the
 119          * exponent).  While the exponent of r is less than or equal
 120          * to (MAX_EXPONENT - 3), the computation should not overflow.
 121          */
 122         java.util.Random rand = new java.util.Random();
 123         for(int i = 0; i < 1000; i++) {
 124             double d = rand.nextDouble();
 125             // Scale d to have an exponent equal to MAX_EXPONENT -15
 126             d = Math.scalb(d, Double.MAX_EXPONENT
 127                                  -15 - Tests.ilogb(d));
 128             for(int j = 0; j <= 13; j += 1) {
 129                 failures += testHypotCase(3*d, 4*d, 5*d, 2.5);
 130                 d *= 2.0; // increase exponent by 1
 131             }
 132         }
 133 
 134         // Test for monotonicity failures.  Fix one argument and test
 135         // two numbers before and two numbers after each chosen value;
 136         // i.e.
 137         //
 138         // pcNeighbors[] =
 139         // {nextDown(nextDown(pc)),
 140         // nextDown(pc),
 141         // pc,
 142         // nextUp(pc),
 143         // nextUp(nextUp(pc))}
 144         //
 145         // and we test that hypot(pcNeighbors[i]) <= hypot(pcNeighbors[i+1])
 146         {