809N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
809N/A *
809N/A * This code is free software; you can redistribute it and/or modify it
809N/A * under the terms of the GNU General Public License version 2 only, as
809N/A * published by the Free Software Foundation.
809N/A *
809N/A * This code is distributed in the hope that it will be useful, but WITHOUT
809N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
809N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
809N/A * version 2 for more details (a copy is included in the LICENSE file that
809N/A * accompanied this code).
809N/A *
809N/A * You should have received a copy of the GNU General Public License version
809N/A * 2 along with this work; if not, write to the Free Software Foundation,
809N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
809N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
809N/A */
809N/A
809N/A/*
809N/A * @test
809N/A * @bug 4851638 4939441
809N/A * @summary Tests for {Math, StrictMath}.hypot
809N/A * @author Joseph D. Darcy
809N/A */
809N/A
809N/Aimport sun.misc.DoubleConsts;
809N/Aimport sun.misc.FpUtils;
809N/A
809N/Apublic class HypotTests {
809N/A private HypotTests(){}
809N/A
809N/A static final double infinityD = Double.POSITIVE_INFINITY;
809N/A static final double NaNd = Double.NaN;
809N/A
809N/A /**
809N/A * Given integers m and n, assuming m < n, the triple (n^2 - m^2,
809N/A * 2mn, and n^2 + m^2) is a Pythagorean triple with a^2 + b^2 =
809N/A * c^2. This methods returns a long array holding the Pythagorean
809N/A * triple corresponding to the inputs.
809N/A */
809N/A static long [] pythagoreanTriple(int m, int n) {
809N/A long M = m;
809N/A long N = n;
809N/A long result[] = new long[3];
809N/A
809N/A
809N/A result[0] = Math.abs(M*M - N*N);
809N/A result[1] = Math.abs(2*M*N);
809N/A result[2] = Math.abs(M*M + N*N);
809N/A
809N/A return result;
809N/A }
809N/A
809N/A static int testHypot() {
809N/A int failures = 0;
809N/A
809N/A double [][] testCases = {
809N/A // Special cases
809N/A {infinityD, infinityD, infinityD},
809N/A {infinityD, 0.0, infinityD},
809N/A {infinityD, 1.0, infinityD},
809N/A {infinityD, NaNd, infinityD},
809N/A {NaNd, NaNd, NaNd},
809N/A {0.0, NaNd, NaNd},
809N/A {1.0, NaNd, NaNd},
809N/A {Double.longBitsToDouble(0x7FF0000000000001L), 1.0, NaNd},
809N/A {Double.longBitsToDouble(0xFFF0000000000001L), 1.0, NaNd},
809N/A {Double.longBitsToDouble(0x7FF8555555555555L), 1.0, NaNd},
809N/A {Double.longBitsToDouble(0xFFF8555555555555L), 1.0, NaNd},
809N/A {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), 1.0, NaNd},
809N/A {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), 1.0, NaNd},
809N/A {Double.longBitsToDouble(0x7FFDeadBeef00000L), 1.0, NaNd},
809N/A {Double.longBitsToDouble(0xFFFDeadBeef00000L), 1.0, NaNd},
809N/A {Double.longBitsToDouble(0x7FFCafeBabe00000L), 1.0, NaNd},
809N/A {Double.longBitsToDouble(0xFFFCafeBabe00000L), 1.0, NaNd},
809N/A };
809N/A
809N/A for(int i = 0; i < testCases.length; i++) {
809N/A failures += testHypotCase(testCases[i][0], testCases[i][1],
809N/A testCases[i][2]);
809N/A }
809N/A
809N/A // Verify hypot(x, 0.0) is close to x over the entire exponent
809N/A // range.
809N/A for(int i = DoubleConsts.MIN_SUB_EXPONENT;
809N/A i <= DoubleConsts.MAX_EXPONENT;
809N/A i++) {
809N/A double input = FpUtils.scalb(2, i);
809N/A failures += testHypotCase(input, 0.0, input);
809N/A }
809N/A
809N/A
809N/A // Test Pythagorean triples
809N/A
809N/A // Small ones
809N/A for(int m = 1; m < 10; m++) {
809N/A for(int n = m+1; n < 11; n++) {
809N/A long [] result = pythagoreanTriple(m, n);
809N/A failures += testHypotCase(result[0], result[1], result[2]);
809N/A }
809N/A }
809N/A
809N/A // Big ones
809N/A for(int m = 100000; m < 100100; m++) {
809N/A for(int n = m+100000; n < 200200; n++) {
809N/A long [] result = pythagoreanTriple(m, n);
809N/A failures += testHypotCase(result[0], result[1], result[2]);
809N/A }
809N/A }
809N/A
809N/A // Approaching overflow tests
809N/A
809N/A /*
809N/A * Create a random value r with an large-ish exponent. The
809N/A * result of hypot(3*r, 4*r) should be approximately 5*r. (The
809N/A * computation of 4*r is exact since it just changes the
809N/A * exponent). While the exponent of r is less than or equal
809N/A * to (MAX_EXPONENT - 3), the computation should not overflow.
809N/A */
809N/A java.util.Random rand = new java.util.Random();
809N/A for(int i = 0; i < 1000; i++) {
809N/A double d = rand.nextDouble();
809N/A // Scale d to have an exponent equal to MAX_EXPONENT -15
809N/A d = FpUtils.scalb(d, DoubleConsts.MAX_EXPONENT
809N/A -15 - FpUtils.ilogb(d));
809N/A for(int j = 0; j <= 13; j += 1) {
809N/A failures += testHypotCase(3*d, 4*d, 5*d, 2.5);
809N/A d *= 2.0; // increase exponent by 1
809N/A }
809N/A }
809N/A
809N/A // Test for monotonicity failures. Fix one argument and test
809N/A // two numbers before and two numbers after each chosen value;
809N/A // i.e.
809N/A //
809N/A // pcNeighbors[] =
809N/A // {nextDown(nextDown(pc)),
809N/A // nextDown(pc),
809N/A // pc,
809N/A // nextUp(pc),
809N/A // nextUp(nextUp(pc))}
809N/A //
809N/A // and we test that hypot(pcNeighbors[i]) <= hypot(pcNeighbors[i+1])
809N/A {
809N/A double pcNeighbors[] = new double[5];
809N/A double pcNeighborsHypot[] = new double[5];
809N/A double pcNeighborsStrictHypot[] = new double[5];
809N/A
809N/A
809N/A for(int i = -18; i <= 18; i++) {
809N/A double pc = FpUtils.scalb(1.0, i);
809N/A
809N/A pcNeighbors[2] = pc;
809N/A pcNeighbors[1] = FpUtils.nextDown(pc);
809N/A pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
809N/A pcNeighbors[3] = FpUtils.nextUp(pc);
809N/A pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]);
809N/A
809N/A for(int j = 0; j < pcNeighbors.length; j++) {
809N/A pcNeighborsHypot[j] = Math.hypot(2.0, pcNeighbors[j]);
809N/A pcNeighborsStrictHypot[j] = StrictMath.hypot(2.0, pcNeighbors[j]);
809N/A }
809N/A
809N/A for(int j = 0; j < pcNeighborsHypot.length-1; j++) {
809N/A if(pcNeighborsHypot[j] > pcNeighborsHypot[j+1] ) {
809N/A failures++;
809N/A System.err.println("Monotonicity failure for Math.hypot on " +
809N/A pcNeighbors[j] + " and " +
809N/A pcNeighbors[j+1] + "\n\treturned " +
809N/A pcNeighborsHypot[j] + " and " +
809N/A pcNeighborsHypot[j+1] );
809N/A }
809N/A
809N/A if(pcNeighborsStrictHypot[j] > pcNeighborsStrictHypot[j+1] ) {
809N/A failures++;
809N/A System.err.println("Monotonicity failure for StrictMath.hypot on " +
809N/A pcNeighbors[j] + " and " +
809N/A pcNeighbors[j+1] + "\n\treturned " +
809N/A pcNeighborsStrictHypot[j] + " and " +
809N/A pcNeighborsStrictHypot[j+1] );
809N/A }
809N/A
809N/A
809N/A }
809N/A
809N/A }
809N/A }
809N/A
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A static int testHypotCase(double input1, double input2, double expected) {
809N/A return testHypotCase(input1,input2, expected, 1);
809N/A }
809N/A
809N/A static int testHypotCase(double input1, double input2, double expected,
809N/A double ulps) {
809N/A int failures = 0;
809N/A if (expected < 0.0) {
809N/A throw new AssertionError("Result of hypot must be greater than " +
809N/A "or equal to zero");
809N/A }
809N/A
809N/A // Test Math and StrictMath methods with no inputs negated,
809N/A // each input negated singly, and both inputs negated. Also
809N/A // test inputs in reversed order.
809N/A
809N/A for(int i = -1; i <= 1; i+=2) {
809N/A for(int j = -1; j <= 1; j+=2) {
809N/A double x = i * input1;
809N/A double y = j * input2;
809N/A failures += Tests.testUlpDiff("Math.hypot", x, y,
809N/A Math.hypot(x, y), expected, ulps);
809N/A failures += Tests.testUlpDiff("Math.hypot", y, x,
809N/A Math.hypot(y, x ), expected, ulps);
809N/A
809N/A failures += Tests.testUlpDiff("StrictMath.hypot", x, y,
809N/A StrictMath.hypot(x, y), expected, ulps);
809N/A failures += Tests.testUlpDiff("StrictMath.hypot", y, x,
809N/A StrictMath.hypot(y, x), expected, ulps);
809N/A }
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A public static void main(String argv[]) {
809N/A int failures = 0;
809N/A
809N/A failures += testHypot();
809N/A
809N/A if (failures > 0) {
809N/A System.err.println("Testing the hypot incurred "
809N/A + failures + " failures.");
809N/A throw new RuntimeException();
809N/A }
809N/A }
809N/A
809N/A}