Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Math/Rint.java
38811 views
/*1* Copyright (c) 1998, 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 4101566 483158926* @summary Check for correct implementation of Math.rint(double)27*/2829import sun.misc.DoubleConsts;3031public class Rint {3233static int testRintCase(double input, double expected) {34int failures = 0;35double result;36failures += Tests.test("Math.rint", input, Math.rint(input), expected);37failures += Tests.test("Math.rint", -input, Math.rint(-input), -expected);38failures += Tests.test("StrictMath.rint",39input, StrictMath.rint(input), expected);40failures += Tests.test("StrictMath.rint", -input,41StrictMath.rint(-input), -expected);42return failures;43}444546public static void main(String args[]) {47int failures = 0;48double twoToThe52 = Math.scalb(1.0, 52); // 2^524950double [][] testCases = {51{0.0, 0.0},52{Double.MIN_VALUE, 0.0},53{Math.nextDown(DoubleConsts.MIN_NORMAL), 0.0},54{DoubleConsts.MIN_NORMAL, 0.0},5556{0.2, 0.0},5758{Math.nextDown(0.5), 0.0},59{ 0.5, 0.0},60{ Math.nextUp(0.5), 1.0},6162{0.7, 1.0},63{Math.nextDown(1.0), 1.0},64{ 1.0, 1.0},65{ Math.nextUp(1.0), 1.0},6667{Math.nextDown(1.5), 1.0},68{ 1.5, 2.0},69{ Math.nextUp(1.5), 2.0},7071{4.2, 4.0},72{4.5, 4.0},73{4.7, 5.0},7475{7.5, 8.0},76{7.2, 7.0},77{7.7, 8.0},7879{150000.75, 150001.0},80{300000.5, 300000.0},81{Math.nextUp(300000.5), 300001.0},82{Math.nextDown(300000.75), 300001.0},83{300000.75, 300001.0},84{Math.nextUp(300000.75), 300001.0},85{300000.99, 300001.0},86{262144.75, 262145.0}, //(2^18 ) + 0.7587{499998.75, 499999.0},88{524287.75, 524288.0}, //(2^19 -1) + 0.7589{524288.75, 524289.0},9091{Math.nextDown(twoToThe52), twoToThe52},92{twoToThe52, twoToThe52},93{Math.nextUp(twoToThe52), Math.nextUp(twoToThe52)},9495{Double.MAX_VALUE, Double.MAX_VALUE},96{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},97{Double.NaN, Double.NaN}9899};100101102for(int i = 0; i < testCases.length; i++) {103failures += testRintCase(testCases[i][0], testCases[i][1]);104}105106// Test values throughout exponent range107for(double d = Double.MIN_VALUE;108d < Double.POSITIVE_INFINITY; d *= 2) {109failures += testRintCase(d, ((d<=0.5)?0.0:d));110}111112if (failures > 0) {113System.err.println("Testing {Math, StrictMath}.rint incurred "114+ failures + " failures.");115throw new RuntimeException();116}117}118}119120121