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 * @library /lib/testlibrary 29 * @build jdk.testlibrary.DoubleUtils jdk.testlibrary.FloatUtils 30 * @run main Rint 31 */ 32 33 public class Rint { 34 35 static int testRintCase(double input, double expected) { 36 int failures = 0; 37 double result; 38 failures += Tests.test("Math.rint", input, Math.rint(input), expected); 39 failures += Tests.test("Math.rint", -input, Math.rint(-input), -expected); 40 failures += Tests.test("StrictMath.rint", 41 input, StrictMath.rint(input), expected); 42 failures += Tests.test("StrictMath.rint", -input, 43 StrictMath.rint(-input), -expected); 44 return failures; 45 } 46 47 48 public static void main(String args[]) { 49 int failures = 0; 50 double twoToThe52 = Math.scalb(1.0, 52); // 2^52 51 52 double [][] testCases = { 53 {0.0, 0.0}, 54 {Double.MIN_VALUE, 0.0}, 55 {Math.nextDown(Double.MIN_NORMAL), 0.0}, 56 {Double.MIN_NORMAL, 0.0}, 57 58 {0.2, 0.0}, 59 60 {Math.nextDown(0.5), 0.0}, 61 { 0.5, 0.0}, 62 { Math.nextUp(0.5), 1.0}, 63 64 {0.7, 1.0}, 65 {Math.nextDown(1.0), 1.0}, 66 { 1.0, 1.0}, 67 { Math.nextUp(1.0), 1.0}, 68 69 {Math.nextDown(1.5), 1.0}, 70 { 1.5, 2.0}, 71 { Math.nextUp(1.5), 2.0}, 72 73 {4.2, 4.0}, 74 {4.5, 4.0}, 75 {4.7, 5.0}, 76 77 {7.5, 8.0}, 78 {7.2, 7.0}, 79 {7.7, 8.0}, 80 81 {150000.75, 150001.0}, 82 {300000.5, 300000.0}, 83 {Math.nextUp(300000.5), 300001.0}, 84 {Math.nextDown(300000.75), 300001.0}, 85 {300000.75, 300001.0}, 86 {Math.nextUp(300000.75), 300001.0}, 87 {300000.99, 300001.0}, 88 {262144.75, 262145.0}, //(2^18 ) + 0.75 89 {499998.75, 499999.0}, 90 {524287.75, 524288.0}, //(2^19 -1) + 0.75 91 {524288.75, 524289.0}, 92 93 {Math.nextDown(twoToThe52), twoToThe52}, 94 {twoToThe52, twoToThe52}, 95 {Math.nextUp(twoToThe52), Math.nextUp(twoToThe52)}, 96 97 {Double.MAX_VALUE, Double.MAX_VALUE}, 98 {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}, 99 {Double.NaN, Double.NaN} 100 101 }; 102 103 104 for(int i = 0; i < testCases.length; i++) { 105 failures += testRintCase(testCases[i][0], testCases[i][1]); 106 } 107 108 // Test values throughout exponent range 109 for(double d = Double.MIN_VALUE; 110 d < Double.POSITIVE_INFINITY; d *= 2) { 111 failures += testRintCase(d, ((d<=0.5)?0.0:d)); 112 } 113 114 if (failures > 0) { 115 System.err.println("Testing {Math, StrictMath}.rint incurred " 116 + failures + " failures."); 117 throw new RuntimeException(); 118 } 119 } 120 }