Path: blob/master/test/jdk/java/lang/Math/Rint.java
41152 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*/2829public class Rint {3031static int testRintCase(double input, double expected) {32int failures = 0;33double result;34failures += Tests.test("Math.rint", input, Math.rint(input), expected);35failures += Tests.test("Math.rint", -input, Math.rint(-input), -expected);36failures += Tests.test("StrictMath.rint",37input, StrictMath.rint(input), expected);38failures += Tests.test("StrictMath.rint", -input,39StrictMath.rint(-input), -expected);40return failures;41}424344public static void main(String args[]) {45int failures = 0;46double twoToThe52 = Math.scalb(1.0, 52); // 2^524748double [][] testCases = {49{0.0, 0.0},50{Double.MIN_VALUE, 0.0},51{Math.nextDown(Double.MIN_NORMAL), 0.0},52{Double.MIN_NORMAL, 0.0},5354{0.2, 0.0},5556{Math.nextDown(0.5), 0.0},57{ 0.5, 0.0},58{ Math.nextUp(0.5), 1.0},5960{0.7, 1.0},61{Math.nextDown(1.0), 1.0},62{ 1.0, 1.0},63{ Math.nextUp(1.0), 1.0},6465{Math.nextDown(1.5), 1.0},66{ 1.5, 2.0},67{ Math.nextUp(1.5), 2.0},6869{4.2, 4.0},70{4.5, 4.0},71{4.7, 5.0},7273{7.5, 8.0},74{7.2, 7.0},75{7.7, 8.0},7677{150000.75, 150001.0},78{300000.5, 300000.0},79{Math.nextUp(300000.5), 300001.0},80{Math.nextDown(300000.75), 300001.0},81{300000.75, 300001.0},82{Math.nextUp(300000.75), 300001.0},83{300000.99, 300001.0},84{262144.75, 262145.0}, //(2^18 ) + 0.7585{499998.75, 499999.0},86{524287.75, 524288.0}, //(2^19 -1) + 0.7587{524288.75, 524289.0},8889{Math.nextDown(twoToThe52), twoToThe52},90{twoToThe52, twoToThe52},91{Math.nextUp(twoToThe52), Math.nextUp(twoToThe52)},9293{Double.MAX_VALUE, Double.MAX_VALUE},94{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},95{Double.NaN, Double.NaN}9697};9899100for(int i = 0; i < testCases.length; i++) {101failures += testRintCase(testCases[i][0], testCases[i][1]);102}103104// Test values throughout exponent range105for(double d = Double.MIN_VALUE;106d < Double.POSITIVE_INFINITY; d *= 2) {107failures += testRintCase(d, ((d<=0.5)?0.0:d));108}109110if (failures > 0) {111System.err.println("Testing {Math, StrictMath}.rint incurred "112+ failures + " failures.");113throw new RuntimeException();114}115}116}117118119