809N/A/*
2362N/A * Copyright (c) 1998, 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 4101566 4831589
809N/A * @summary Check for correct implementation of Math.rint(double)
809N/A *
809N/A */
809N/A
809N/Aimport sun.misc.FpUtils;
809N/Aimport sun.misc.DoubleConsts;
809N/A
809N/Apublic class Rint {
809N/A
809N/A static int testRintCase(double input, double expected) {
809N/A int failures = 0;
809N/A double result;
809N/A failures += Tests.test("Math.rint", input, Math.rint(input), expected);
809N/A failures += Tests.test("Math.rint", -input, Math.rint(-input), -expected);
809N/A failures += Tests.test("StrictMath.rint",
809N/A input, StrictMath.rint(input), expected);
809N/A failures += Tests.test("StrictMath.rint", -input,
809N/A StrictMath.rint(-input), -expected);
809N/A return failures;
809N/A }
809N/A
809N/A
809N/A public static void main(String args[]) {
809N/A int failures = 0;
809N/A double twoToThe52 = FpUtils.scalb(1.0, 52); // 2^52
809N/A
809N/A double [][] testCases = {
809N/A {0.0, 0.0},
809N/A {Double.MIN_VALUE, 0.0},
809N/A {FpUtils.nextDown(DoubleConsts.MIN_NORMAL), 0.0},
809N/A {DoubleConsts.MIN_NORMAL, 0.0},
809N/A
809N/A {0.2, 0.0},
809N/A
809N/A {FpUtils.nextDown(0.5), 0.0},
809N/A { 0.5, 0.0},
809N/A { FpUtils.nextUp(0.5), 1.0},
809N/A
809N/A {0.7, 1.0},
809N/A {FpUtils.nextDown(1.0), 1.0},
809N/A { 1.0, 1.0},
809N/A { FpUtils.nextUp(1.0), 1.0},
809N/A
809N/A {FpUtils.nextDown(1.5), 1.0},
809N/A { 1.5, 2.0},
809N/A { FpUtils.nextUp(1.5), 2.0},
809N/A
809N/A {4.2, 4.0},
809N/A {4.5, 4.0},
809N/A {4.7, 5.0},
809N/A
809N/A {7.5, 8.0},
809N/A {7.2, 7.0},
809N/A {7.7, 8.0},
809N/A
809N/A {150000.75, 150001.0},
809N/A {300000.5, 300000.0},
809N/A {FpUtils.nextUp(300000.5), 300001.0},
809N/A {FpUtils.nextDown(300000.75), 300001.0},
809N/A {300000.75, 300001.0},
809N/A {FpUtils.nextUp(300000.75), 300001.0},
809N/A {300000.99, 300001.0},
809N/A {262144.75, 262145.0}, //(2^18 ) + 0.75
809N/A {499998.75, 499999.0},
809N/A {524287.75, 524288.0}, //(2^19 -1) + 0.75
809N/A {524288.75, 524289.0},
809N/A
809N/A {FpUtils.nextDown(twoToThe52), twoToThe52},
809N/A {twoToThe52, twoToThe52},
809N/A {FpUtils.nextUp(twoToThe52), FpUtils.nextUp(twoToThe52)},
809N/A
809N/A {Double.MAX_VALUE, Double.MAX_VALUE},
809N/A {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},
809N/A {Double.NaN, Double.NaN}
809N/A
809N/A };
809N/A
809N/A
809N/A for(int i = 0; i < testCases.length; i++) {
809N/A failures += testRintCase(testCases[i][0], testCases[i][1]);
809N/A }
809N/A
809N/A // Test values throughout exponent range
809N/A for(double d = Double.MIN_VALUE;
809N/A d < Double.POSITIVE_INFINITY; d *= 2) {
809N/A failures += testRintCase(d, ((d<=0.5)?0.0:d));
809N/A }
809N/A
809N/A if (failures > 0) {
809N/A System.err.println("Testing {Math, StrictMath}.rint incurred "
809N/A + failures + " failures.");
809N/A throw new RuntimeException();
809N/A }
809N/A }
809N/A
809N/A}