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  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.DoubleUtils jdk.testlibrary.FloatUtils
  30  * @run main HypotTests
  31  * @author Joseph D. Darcy
  32  */
  33 
  34 import static jdk.testlibrary.DoubleUtils.*;
  35 
  36 public class HypotTests {
  37     private HypotTests(){}
  38 
  39     static final double infinityD = Double.POSITIVE_INFINITY;
  40     static final double NaNd      = Double.NaN;
  41 
  42     /**
  43      * Given integers m and n, assuming m < n, the triple (n^2 - m^2,
  44      * 2mn, and n^2 + m^2) is a Pythagorean triple with a^2 + b^2 =
  45      * c^2.  This methods returns a long array holding the Pythagorean
  46      * triple corresponding to the inputs.
  47      */
  48     static long [] pythagoreanTriple(int m, int n) {
  49         long M = m;
  50         long N = n;
  51         long result[] = new long[3];
  52 
  53 
  54         result[0] = Math.abs(M*M - N*N);
  55         result[1] = Math.abs(2*M*N);
  56         result[2] = Math.abs(M*M + N*N);
  57 
  58         return result;
  59     }
  60 
  61     static int testHypot() {
  62         int failures = 0;
  63 
  64         double [][] testCases = {
  65             // Special cases
  66             {infinityD,         infinityD,              infinityD},
  67             {infinityD,         0.0,                    infinityD},
  68             {infinityD,         1.0,                    infinityD},
  69             {infinityD,         NaNd,                   infinityD},
  70             {NaNd,              NaNd,                   NaNd},
  71             {0.0,               NaNd,                   NaNd},
  72             {1.0,               NaNd,                   NaNd},
  73             {Double.longBitsToDouble(0x7FF0000000000001L),      1.0,    NaNd},
  74             {Double.longBitsToDouble(0xFFF0000000000001L),      1.0,    NaNd},
  75             {Double.longBitsToDouble(0x7FF8555555555555L),      1.0,    NaNd},
  76             {Double.longBitsToDouble(0xFFF8555555555555L),      1.0,    NaNd},
  77             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      1.0,    NaNd},
  78             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      1.0,    NaNd},
  79             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      1.0,    NaNd},
  80             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      1.0,    NaNd},
  81             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      1.0,    NaNd},
  82             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      1.0,    NaNd},
  83         };
  84 
  85         for(int i = 0; i < testCases.length; i++) {
  86             failures += testHypotCase(testCases[i][0], testCases[i][1],
  87                                       testCases[i][2]);
  88         }
  89 
  90         // Verify hypot(x, 0.0) is close to x over the entire exponent
  91         // range.
  92         for(int i = MIN_SUB_EXPONENT;
  93             i <= Double.MAX_EXPONENT;
  94             i++) {
  95             double input = Math.scalb(2, i);
  96             failures += testHypotCase(input, 0.0, input);
  97         }
  98 
  99 
 100         // Test Pythagorean triples
 101 
 102         // Small ones
 103         for(int m = 1; m < 10; m++) {
 104             for(int n = m+1; n < 11; n++) {
 105                 long [] result = pythagoreanTriple(m, n);
 106                 failures += testHypotCase(result[0], result[1], result[2]);
 107             }
 108         }
 109 
 110         // Big ones
 111         for(int m = 100000; m < 100100; m++) {
 112             for(int n = m+100000; n < 200200; n++) {
 113                 long [] result = pythagoreanTriple(m, n);
 114                 failures += testHypotCase(result[0], result[1], result[2]);
 115             }
 116         }
 117 
 118         // Approaching overflow tests
 119 
 120         /*
 121          * Create a random value r with an large-ish exponent.  The
 122          * result of hypot(3*r, 4*r) should be approximately 5*r. (The
 123          * computation of 4*r is exact since it just changes the
 124          * exponent).  While the exponent of r is less than or equal
 125          * to (MAX_EXPONENT - 3), the computation should not overflow.
 126          */
 127         java.util.Random rand = new java.util.Random();
 128         for(int i = 0; i < 1000; i++) {
 129             double d = rand.nextDouble();
 130             // Scale d to have an exponent equal to MAX_EXPONENT -15
 131             d = Math.scalb(d, Double.MAX_EXPONENT
 132                                  -15 - Tests.ilogb(d));
 133             for(int j = 0; j <= 13; j += 1) {
 134                 failures += testHypotCase(3*d, 4*d, 5*d, 2.5);
 135                 d *= 2.0; // increase exponent by 1
 136             }
 137         }
 138 
 139         // Test for monotonicity failures.  Fix one argument and test
 140         // two numbers before and two numbers after each chosen value;
 141         // i.e.
 142         //
 143         // pcNeighbors[] =
 144         // {nextDown(nextDown(pc)),
 145         // nextDown(pc),
 146         // pc,
 147         // nextUp(pc),
 148         // nextUp(nextUp(pc))}
 149         //
 150         // and we test that hypot(pcNeighbors[i]) <= hypot(pcNeighbors[i+1])
 151         {
 152             double pcNeighbors[] = new double[5];
 153             double pcNeighborsHypot[] = new double[5];
 154             double pcNeighborsStrictHypot[] = new double[5];
 155 
 156 
 157             for(int i = -18; i <= 18; i++) {
 158                 double pc = Math.scalb(1.0, i);
 159 
 160                 pcNeighbors[2] = pc;
 161                 pcNeighbors[1] = Math.nextDown(pc);
 162                 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);
 163                 pcNeighbors[3] = Math.nextUp(pc);
 164                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
 165 
 166                 for(int j = 0; j < pcNeighbors.length; j++) {
 167                     pcNeighborsHypot[j]       =       Math.hypot(2.0, pcNeighbors[j]);
 168                     pcNeighborsStrictHypot[j] = StrictMath.hypot(2.0, pcNeighbors[j]);
 169                 }
 170 
 171                 for(int j = 0; j < pcNeighborsHypot.length-1; j++) {
 172                     if(pcNeighborsHypot[j] >  pcNeighborsHypot[j+1] ) {
 173                         failures++;
 174                         System.err.println("Monotonicity failure for Math.hypot on " +
 175                                           pcNeighbors[j] + " and "  +
 176                                           pcNeighbors[j+1] + "\n\treturned " +
 177                                           pcNeighborsHypot[j] + " and " +
 178                                           pcNeighborsHypot[j+1] );
 179                     }
 180 
 181                     if(pcNeighborsStrictHypot[j] >  pcNeighborsStrictHypot[j+1] ) {
 182                         failures++;
 183                         System.err.println("Monotonicity failure for StrictMath.hypot on " +
 184                                           pcNeighbors[j] + " and "  +
 185                                           pcNeighbors[j+1] + "\n\treturned " +
 186                                           pcNeighborsStrictHypot[j] + " and " +
 187                                           pcNeighborsStrictHypot[j+1] );
 188                     }
 189 
 190 
 191                 }
 192 
 193             }
 194         }
 195 
 196 
 197         return failures;
 198     }
 199 
 200     static int testHypotCase(double input1, double input2, double expected) {
 201         return testHypotCase(input1,input2, expected, 1);
 202     }
 203 
 204     static int testHypotCase(double input1, double input2, double expected,
 205                              double ulps) {
 206         int failures = 0;
 207         if (expected < 0.0) {
 208             throw new AssertionError("Result of hypot must be greater than " +
 209                                      "or equal to zero");
 210         }
 211 
 212         // Test Math and StrictMath methods with no inputs negated,
 213         // each input negated singly, and both inputs negated.  Also
 214         // test inputs in reversed order.
 215 
 216         for(int i = -1; i <= 1; i+=2) {
 217             for(int j = -1; j <= 1; j+=2) {
 218                 double x = i * input1;
 219                 double y = j * input2;
 220                 failures += Tests.testUlpDiff("Math.hypot", x, y,
 221                                               Math.hypot(x, y), expected, ulps);
 222                 failures += Tests.testUlpDiff("Math.hypot", y, x,
 223                                               Math.hypot(y, x ), expected, ulps);
 224 
 225                 failures += Tests.testUlpDiff("StrictMath.hypot", x, y,
 226                                               StrictMath.hypot(x, y), expected, ulps);
 227                 failures += Tests.testUlpDiff("StrictMath.hypot", y, x,
 228                                               StrictMath.hypot(y, x), expected, ulps);
 229             }
 230         }
 231 
 232         return failures;
 233     }
 234 
 235     public static void main(String argv[]) {
 236         int failures = 0;
 237 
 238         failures += testHypot();
 239 
 240         if (failures > 0) {
 241             System.err.println("Testing the hypot incurred "
 242                                + failures + " failures.");
 243             throw new RuntimeException();
 244         }
 245     }
 246 
 247 }