Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Math/HypotTests.java
38812 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4851638 493944126* @summary Tests for {Math, StrictMath}.hypot27* @author Joseph D. Darcy28* @key randomness29*/3031import sun.misc.DoubleConsts;32import sun.misc.FpUtils;3334public class HypotTests {35private HypotTests(){}3637static final double infinityD = Double.POSITIVE_INFINITY;38static final double NaNd = Double.NaN;3940/**41* Given integers m and n, assuming m < n, the triple (n^2 - m^2,42* 2mn, and n^2 + m^2) is a Pythagorean triple with a^2 + b^2 =43* c^2. This methods returns a long array holding the Pythagorean44* triple corresponding to the inputs.45*/46static long [] pythagoreanTriple(int m, int n) {47long M = m;48long N = n;49long result[] = new long[3];505152result[0] = Math.abs(M*M - N*N);53result[1] = Math.abs(2*M*N);54result[2] = Math.abs(M*M + N*N);5556return result;57}5859static int testHypot() {60int failures = 0;6162double [][] testCases = {63// Special cases64{infinityD, infinityD, infinityD},65{infinityD, 0.0, infinityD},66{infinityD, 1.0, infinityD},67{infinityD, NaNd, infinityD},68{NaNd, NaNd, NaNd},69{0.0, NaNd, NaNd},70{1.0, NaNd, NaNd},71{Double.longBitsToDouble(0x7FF0000000000001L), 1.0, NaNd},72{Double.longBitsToDouble(0xFFF0000000000001L), 1.0, NaNd},73{Double.longBitsToDouble(0x7FF8555555555555L), 1.0, NaNd},74{Double.longBitsToDouble(0xFFF8555555555555L), 1.0, NaNd},75{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), 1.0, NaNd},76{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), 1.0, NaNd},77{Double.longBitsToDouble(0x7FFDeadBeef00000L), 1.0, NaNd},78{Double.longBitsToDouble(0xFFFDeadBeef00000L), 1.0, NaNd},79{Double.longBitsToDouble(0x7FFCafeBabe00000L), 1.0, NaNd},80{Double.longBitsToDouble(0xFFFCafeBabe00000L), 1.0, NaNd},81};8283for(int i = 0; i < testCases.length; i++) {84failures += testHypotCase(testCases[i][0], testCases[i][1],85testCases[i][2]);86}8788// Verify hypot(x, 0.0) is close to x over the entire exponent89// range.90for(int i = DoubleConsts.MIN_SUB_EXPONENT;91i <= DoubleConsts.MAX_EXPONENT;92i++) {93double input = Math.scalb(2, i);94failures += testHypotCase(input, 0.0, input);95}969798// Test Pythagorean triples99100// Small ones101for(int m = 1; m < 10; m++) {102for(int n = m+1; n < 11; n++) {103long [] result = pythagoreanTriple(m, n);104failures += testHypotCase(result[0], result[1], result[2]);105}106}107108// Big ones109for(int m = 100000; m < 100100; m++) {110for(int n = m+100000; n < 200200; n++) {111long [] result = pythagoreanTriple(m, n);112failures += testHypotCase(result[0], result[1], result[2]);113}114}115116// Approaching overflow tests117118/*119* Create a random value r with an large-ish exponent. The120* result of hypot(3*r, 4*r) should be approximately 5*r. (The121* computation of 4*r is exact since it just changes the122* exponent). While the exponent of r is less than or equal123* to (MAX_EXPONENT - 3), the computation should not overflow.124*/125java.util.Random rand = new java.util.Random();126for(int i = 0; i < 1000; i++) {127double d = rand.nextDouble();128// Scale d to have an exponent equal to MAX_EXPONENT -15129d = Math.scalb(d, DoubleConsts.MAX_EXPONENT130-15 - FpUtils.ilogb(d));131for(int j = 0; j <= 13; j += 1) {132failures += testHypotCase(3*d, 4*d, 5*d, 2.5);133d *= 2.0; // increase exponent by 1134}135}136137// Test for monotonicity failures. Fix one argument and test138// two numbers before and two numbers after each chosen value;139// i.e.140//141// pcNeighbors[] =142// {nextDown(nextDown(pc)),143// nextDown(pc),144// pc,145// nextUp(pc),146// nextUp(nextUp(pc))}147//148// and we test that hypot(pcNeighbors[i]) <= hypot(pcNeighbors[i+1])149{150double pcNeighbors[] = new double[5];151double pcNeighborsHypot[] = new double[5];152double pcNeighborsStrictHypot[] = new double[5];153154155for(int i = -18; i <= 18; i++) {156double pc = Math.scalb(1.0, i);157158pcNeighbors[2] = pc;159pcNeighbors[1] = Math.nextDown(pc);160pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);161pcNeighbors[3] = Math.nextUp(pc);162pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);163164for(int j = 0; j < pcNeighbors.length; j++) {165pcNeighborsHypot[j] = Math.hypot(2.0, pcNeighbors[j]);166pcNeighborsStrictHypot[j] = StrictMath.hypot(2.0, pcNeighbors[j]);167}168169for(int j = 0; j < pcNeighborsHypot.length-1; j++) {170if(pcNeighborsHypot[j] > pcNeighborsHypot[j+1] ) {171failures++;172System.err.println("Monotonicity failure for Math.hypot on " +173pcNeighbors[j] + " and " +174pcNeighbors[j+1] + "\n\treturned " +175pcNeighborsHypot[j] + " and " +176pcNeighborsHypot[j+1] );177}178179if(pcNeighborsStrictHypot[j] > pcNeighborsStrictHypot[j+1] ) {180failures++;181System.err.println("Monotonicity failure for StrictMath.hypot on " +182pcNeighbors[j] + " and " +183pcNeighbors[j+1] + "\n\treturned " +184pcNeighborsStrictHypot[j] + " and " +185pcNeighborsStrictHypot[j+1] );186}187188189}190191}192}193194195return failures;196}197198static int testHypotCase(double input1, double input2, double expected) {199return testHypotCase(input1,input2, expected, 1);200}201202static int testHypotCase(double input1, double input2, double expected,203double ulps) {204int failures = 0;205if (expected < 0.0) {206throw new AssertionError("Result of hypot must be greater than " +207"or equal to zero");208}209210// Test Math and StrictMath methods with no inputs negated,211// each input negated singly, and both inputs negated. Also212// test inputs in reversed order.213214for(int i = -1; i <= 1; i+=2) {215for(int j = -1; j <= 1; j+=2) {216double x = i * input1;217double y = j * input2;218failures += Tests.testUlpDiff("Math.hypot", x, y,219Math.hypot(x, y), expected, ulps);220failures += Tests.testUlpDiff("Math.hypot", y, x,221Math.hypot(y, x ), expected, ulps);222223failures += Tests.testUlpDiff("StrictMath.hypot", x, y,224StrictMath.hypot(x, y), expected, ulps);225failures += Tests.testUlpDiff("StrictMath.hypot", y, x,226StrictMath.hypot(y, x), expected, ulps);227}228}229230return failures;231}232233public static void main(String argv[]) {234int failures = 0;235236failures += testHypot();237238if (failures > 0) {239System.err.println("Testing the hypot incurred "240+ failures + " failures.");241throw new RuntimeException();242}243}244245}246247248