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];
  48 
  49 
  50         result[0] = Math.abs(M*M - N*N);
  51         result[1] = Math.abs(2*M*N);
  52         result[2] = Math.abs(M*M + N*N);
  53 
  54         return result;
  55     }
  56 
  57     static int testHypot() {
  58         int failures = 0;
  59 
  60         double [][] testCases = {
  61             // Special cases
  62             {infinityD,         infinityD,              infinityD},
  63             {infinityD,         0.0,                    infinityD},
  64             {infinityD,         1.0,                    infinityD},
  65             {infinityD,         NaNd,                   infinityD},
  66             {NaNd,              NaNd,                   NaNd},
  67             {0.0,               NaNd,                   NaNd},
  68             {1.0,               NaNd,                   NaNd},
  69             {Double.longBitsToDouble(0x7FF0000000000001L),      1.0,    NaNd},
  70             {Double.longBitsToDouble(0xFFF0000000000001L),      1.0,    NaNd},
  71             {Double.longBitsToDouble(0x7FF8555555555555L),      1.0,    NaNd},
  72             {Double.longBitsToDouble(0xFFF8555555555555L),      1.0,    NaNd},
  73             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      1.0,    NaNd},
  74             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      1.0,    NaNd},
  75             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      1.0,    NaNd},
  76             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      1.0,    NaNd},
  77             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      1.0,    NaNd},
  78             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      1.0,    NaNd},
  79         };
  80 
  81         for(int i = 0; i < testCases.length; i++) {
  82             failures += testHypotCase(testCases[i][0], testCases[i][1],
  83                                       testCases[i][2]);
  84         }
  85 
  86         // Verify hypot(x, 0.0) is close to x over the entire exponent
  87         // range.
  88         for(int i = DoubleConsts.MIN_SUB_EXPONENT;
  89             i <= Double.MAX_EXPONENT;
  90             i++) {
  91             double input = Math.scalb(2, i);
  92             failures += testHypotCase(input, 0.0, input);
  93         }
  94 
  95 
  96         // Test Pythagorean triples
  97 
  98         // Small ones
  99         for(int m = 1; m < 10; m++) {
 100             for(int n = m+1; n < 11; n++) {
 101                 long [] result = pythagoreanTriple(m, n);
 102                 failures += testHypotCase(result[0], result[1], result[2]);
 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),
 144         // nextUp(nextUp(pc))}
 145         //
 146         // and we test that hypot(pcNeighbors[i]) <= hypot(pcNeighbors[i+1])
 147         {
 148             double pcNeighbors[] = new double[5];
 149             double pcNeighborsHypot[] = new double[5];
 150             double pcNeighborsStrictHypot[] = new double[5];
 151 
 152 
 153             for(int i = -18; i <= 18; i++) {
 154                 double pc = Math.scalb(1.0, i);
 155 
 156                 pcNeighbors[2] = pc;
 157                 pcNeighbors[1] = Math.nextDown(pc);
 158                 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);
 159                 pcNeighbors[3] = Math.nextUp(pc);
 160                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
 161 
 162                 for(int j = 0; j < pcNeighbors.length; j++) {
 163                     pcNeighborsHypot[j]       =       Math.hypot(2.0, pcNeighbors[j]);
 164                     pcNeighborsStrictHypot[j] = StrictMath.hypot(2.0, pcNeighbors[j]);
 165                 }
 166 
 167                 for(int j = 0; j < pcNeighborsHypot.length-1; j++) {
 168                     if(pcNeighborsHypot[j] >  pcNeighborsHypot[j+1] ) {
 169                         failures++;
 170                         System.err.println("Monotonicity failure for Math.hypot on " +
 171                                           pcNeighbors[j] + " and "  +
 172                                           pcNeighbors[j+1] + "\n\treturned " +
 173                                           pcNeighborsHypot[j] + " and " +
 174                                           pcNeighborsHypot[j+1] );
 175                     }
 176 
 177                     if(pcNeighborsStrictHypot[j] >  pcNeighborsStrictHypot[j+1] ) {
 178                         failures++;
 179                         System.err.println("Monotonicity failure for StrictMath.hypot on " +
 180                                           pcNeighbors[j] + " and "  +
 181                                           pcNeighbors[j+1] + "\n\treturned " +
 182                                           pcNeighborsStrictHypot[j] + " and " +
 183                                           pcNeighborsStrictHypot[j+1] );
 184                     }
 185 
 186 
 187                 }
 188 
 189             }
 190         }
 191 
 192 
 193         return failures;
 194     }
 195 
 196     static int testHypotCase(double input1, double input2, double expected) {
 197         return testHypotCase(input1,input2, expected, 1);
 198     }
 199 
 200     static int testHypotCase(double input1, double input2, double expected,
 201                              double ulps) {
 202         int failures = 0;
 203         if (expected < 0.0) {
 204             throw new AssertionError("Result of hypot must be greater than " +
 205                                      "or equal to zero");
 206         }
 207 
 208         // Test Math and StrictMath methods with no inputs negated,
 209         // each input negated singly, and both inputs negated.  Also
 210         // test inputs in reversed order.
 211 
 212         for(int i = -1; i <= 1; i+=2) {
 213             for(int j = -1; j <= 1; j+=2) {
 214                 double x = i * input1;
 215                 double y = j * input2;
 216                 failures += Tests.testUlpDiff("Math.hypot", x, y,
 217                                               Math.hypot(x, y), expected, ulps);
 218                 failures += Tests.testUlpDiff("Math.hypot", y, x,
 219                                               Math.hypot(y, x ), expected, ulps);
 220 
 221                 failures += Tests.testUlpDiff("StrictMath.hypot", x, y,
 222                                               StrictMath.hypot(x, y), expected, ulps);
 223                 failures += Tests.testUlpDiff("StrictMath.hypot", y, x,
 224                                               StrictMath.hypot(y, x), expected, ulps);
 225             }
 226         }
 227 
 228         return failures;
 229     }
 230 
 231     public static void main(String argv[]) {
 232         int failures = 0;
 233 
 234         failures += testHypot();
 235 
 236         if (failures > 0) {
 237             System.err.println("Testing the hypot incurred "
 238                                + failures + " failures.");
 239             throw new RuntimeException();
 240         }
 241     }
 242 
 243 }