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 package test.java.lang.Math;
30 
31 import org.testng.annotations.Test;
32 
33 public class Rint {
34 
testRintCase(double input, double expected)35     static void testRintCase(double input, double expected) {
36         Tests.test("Math.rint", input, Math.rint(input), expected);
37         Tests.test("Math.rint", -input, Math.rint(-input), -expected);
38         Tests.test("StrictMath.rint",
39                 input, StrictMath.rint(input), expected);
40         Tests.test("StrictMath.rint", -input,
41                 StrictMath.rint(-input), -expected);
42     }
43 
44     @Test
testRint()45     public void testRint() {
46         double twoToThe52 = Math.scalb(1.0, 52); // 2^52
47 
48         double[][] 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},
53 
54                 {0.2, 0.0},
55 
56                 {Math.nextDown(0.5), 0.0},
57                 {0.5, 0.0},
58                 {Math.nextUp(0.5), 1.0},
59 
60                 {0.7, 1.0},
61                 {Math.nextDown(1.0), 1.0},
62                 {1.0, 1.0},
63                 {Math.nextUp(1.0), 1.0},
64 
65                 {Math.nextDown(1.5), 1.0},
66                 {1.5, 2.0},
67                 {Math.nextUp(1.5), 2.0},
68 
69                 {4.2, 4.0},
70                 {4.5, 4.0},
71                 {4.7, 5.0},
72 
73                 {7.5, 8.0},
74                 {7.2, 7.0},
75                 {7.7, 8.0},
76 
77                 {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.75
85                 {499998.75, 499999.0},
86                 {524287.75, 524288.0}, //(2^19 -1) + 0.75
87                 {524288.75, 524289.0},
88 
89                 {Math.nextDown(twoToThe52), twoToThe52},
90                 {twoToThe52, twoToThe52},
91                 {Math.nextUp(twoToThe52), Math.nextUp(twoToThe52)},
92 
93                 {Double.MAX_VALUE, Double.MAX_VALUE},
94                 {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},
95                 {Double.NaN, Double.NaN}
96 
97         };
98 
99         for (double[] testCase : testCases) {
100             testRintCase(testCase[0], testCase[1]);
101         }
102 
103         // Test values throughout exponent range
104         for (double d = Double.MIN_VALUE;
105                 d < Double.POSITIVE_INFINITY; d *= 2) {
106             testRintCase(d, ((d <= 0.5) ? 0.0 : d));
107         }
108     }
109 }
110