Rint.java revision 809
3972N/A/*
3972N/A * Copyright 1998-2003 Sun Microsystems, Inc. All Rights Reserved.
3972N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3972N/A *
3972N/A * This code is free software; you can redistribute it and/or modify it
3972N/A * under the terms of the GNU General Public License version 2 only, as
3972N/A * published by the Free Software Foundation.
3972N/A *
3972N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3972N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3972N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3972N/A * version 2 for more details (a copy is included in the LICENSE file that
3972N/A * accompanied this code).
3972N/A *
3972N/A * You should have received a copy of the GNU General Public License version
3972N/A * 2 along with this work; if not, write to the Free Software Foundation,
3972N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3972N/A *
3972N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3972N/A * CA 95054 USA or visit www.sun.com if you need additional information or
3972N/A * have any questions.
3972N/A */
3972N/A
3972N/A/*
3972N/A * @test
3972N/A * @bug 4101566 4831589
3972N/A * @summary Check for correct implementation of Math.rint(double)
3972N/A *
3972N/A */
3972N/A
3972N/Aimport sun.misc.FpUtils;
3972N/Aimport sun.misc.DoubleConsts;
3972N/A
3972N/Apublic class Rint {
3972N/A
3972N/A static int testRintCase(double input, double expected) {
3972N/A int failures = 0;
3972N/A double result;
3972N/A failures += Tests.test("Math.rint", input, Math.rint(input), expected);
3972N/A failures += Tests.test("Math.rint", -input, Math.rint(-input), -expected);
3972N/A failures += Tests.test("StrictMath.rint",
3972N/A input, StrictMath.rint(input), expected);
3972N/A failures += Tests.test("StrictMath.rint", -input,
3972N/A StrictMath.rint(-input), -expected);
3972N/A return failures;
3972N/A }
3972N/A
3972N/A
3972N/A public static void main(String args[]) {
3972N/A int failures = 0;
3972N/A double twoToThe52 = FpUtils.scalb(1.0, 52); // 2^52
3972N/A
3972N/A double [][] testCases = {
3972N/A {0.0, 0.0},
3972N/A {Double.MIN_VALUE, 0.0},
3972N/A {FpUtils.nextDown(DoubleConsts.MIN_NORMAL), 0.0},
3972N/A {DoubleConsts.MIN_NORMAL, 0.0},
3972N/A
3972N/A {0.2, 0.0},
3972N/A
3972N/A {FpUtils.nextDown(0.5), 0.0},
3972N/A { 0.5, 0.0},
3972N/A { FpUtils.nextUp(0.5), 1.0},
{0.7, 1.0},
{FpUtils.nextDown(1.0), 1.0},
{ 1.0, 1.0},
{ FpUtils.nextUp(1.0), 1.0},
{FpUtils.nextDown(1.5), 1.0},
{ 1.5, 2.0},
{ FpUtils.nextUp(1.5), 2.0},
{4.2, 4.0},
{4.5, 4.0},
{4.7, 5.0},
{7.5, 8.0},
{7.2, 7.0},
{7.7, 8.0},
{150000.75, 150001.0},
{300000.5, 300000.0},
{FpUtils.nextUp(300000.5), 300001.0},
{FpUtils.nextDown(300000.75), 300001.0},
{300000.75, 300001.0},
{FpUtils.nextUp(300000.75), 300001.0},
{300000.99, 300001.0},
{262144.75, 262145.0}, //(2^18 ) + 0.75
{499998.75, 499999.0},
{524287.75, 524288.0}, //(2^19 -1) + 0.75
{524288.75, 524289.0},
{FpUtils.nextDown(twoToThe52), twoToThe52},
{twoToThe52, twoToThe52},
{FpUtils.nextUp(twoToThe52), FpUtils.nextUp(twoToThe52)},
{Double.MAX_VALUE, Double.MAX_VALUE},
{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},
{Double.NaN, Double.NaN}
};
for(int i = 0; i < testCases.length; i++) {
failures += testRintCase(testCases[i][0], testCases[i][1]);
}
// Test values throughout exponent range
for(double d = Double.MIN_VALUE;
d < Double.POSITIVE_INFINITY; d *= 2) {
failures += testRintCase(d, ((d<=0.5)?0.0:d));
}
if (failures > 0) {
System.err.println("Testing {Math, StrictMath}.rint incurred "
+ failures + " failures.");
throw new RuntimeException();
}
}
}