1 /* 2 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4101566 4831589 27 * @summary Check for correct implementation of Math.rint(double) 28 */ 29 30 import sun.misc.DoubleConsts; 31 32 public class Rint { 33 34 static int testRintCase(double input, double expected) { 35 int failures = 0; 36 double result; 37 failures += Tests.test("Math.rint", input, Math.rint(input), expected); 38 failures += Tests.test("Math.rint", -input, Math.rint(-input), -expected); 39 failures += Tests.test("StrictMath.rint", 40 input, StrictMath.rint(input), expected); 41 failures += Tests.test("StrictMath.rint", -input, 42 StrictMath.rint(-input), -expected); 43 return failures; 44 } 45 46 47 public static void main(String args[]) { 48 int failures = 0; 49 double twoToThe52 = Math.scalb(1.0, 52); // 2^52 50 51 double [][] testCases = { 52 {0.0, 0.0}, 53 {Double.MIN_VALUE, 0.0}, 54 {Math.nextDown(DoubleConsts.MIN_NORMAL), 0.0}, 55 {DoubleConsts.MIN_NORMAL, 0.0}, 56 57 {0.2, 0.0}, 58 59 {Math.nextDown(0.5), 0.0}, 60 { 0.5, 0.0}, 61 { Math.nextUp(0.5), 1.0}, 62 63 {0.7, 1.0}, 64 {Math.nextDown(1.0), 1.0}, 65 { 1.0, 1.0}, 66 { Math.nextUp(1.0), 1.0}, 67 68 {Math.nextDown(1.5), 1.0}, 69 { 1.5, 2.0}, 70 { Math.nextUp(1.5), 2.0}, 71 72 {4.2, 4.0}, 73 {4.5, 4.0}, 74 {4.7, 5.0}, 75 76 {7.5, 8.0}, 77 {7.2, 7.0}, 78 {7.7, 8.0}, 79 80 {150000.75, 150001.0}, 81 {300000.5, 300000.0}, 82 {Math.nextUp(300000.5), 300001.0}, 83 {Math.nextDown(300000.75), 300001.0}, 84 {300000.75, 300001.0}, 85 {Math.nextUp(300000.75), 300001.0}, 86 {300000.99, 300001.0}, 87 {262144.75, 262145.0}, //(2^18 ) + 0.75 88 {499998.75, 499999.0}, 89 {524287.75, 524288.0}, //(2^19 -1) + 0.75 90 {524288.75, 524289.0}, 91 92 {Math.nextDown(twoToThe52), twoToThe52}, 93 {twoToThe52, twoToThe52}, 94 {Math.nextUp(twoToThe52), Math.nextUp(twoToThe52)}, 95 96 {Double.MAX_VALUE, Double.MAX_VALUE}, 97 {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}, 98 {Double.NaN, Double.NaN} 99 100 }; 101 102 103 for(int i = 0; i < testCases.length; i++) { 104 failures += testRintCase(testCases[i][0], testCases[i][1]); 105 } 106 107 // Test values throughout exponent range 108 for(double d = Double.MIN_VALUE; 109 d < Double.POSITIVE_INFINITY; d *= 2) { 110 failures += testRintCase(d, ((d<=0.5)?0.0:d)); 111 } 112 113 if (failures > 0) { 114 System.err.println("Testing {Math, StrictMath}.rint incurred " 115 + failures + " failures."); 116 throw new RuntimeException(); 117 } 118 } 119 }