blob: d8e35cd134369ae0a96a05871e2f0c77807ba68b [file] [log] [blame]
darcy32db4492009-01-26 19:49:26 -08001/*
2 * Copyright 1998-2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4101566 4831589
27 * @summary Check for correct implementation of Math.rint(double)
28 *
29 */
30
31import sun.misc.FpUtils;
32import sun.misc.DoubleConsts;
33
34public class Rint {
35
36 static int testRintCase(double input, double expected) {
37 int failures = 0;
38 double result;
39 failures += Tests.test("Math.rint", input, Math.rint(input), expected);
40 failures += Tests.test("Math.rint", -input, Math.rint(-input), -expected);
41 failures += Tests.test("StrictMath.rint",
42 input, StrictMath.rint(input), expected);
43 failures += Tests.test("StrictMath.rint", -input,
44 StrictMath.rint(-input), -expected);
45 return failures;
46 }
47
48
49 public static void main(String args[]) {
50 int failures = 0;
51 double twoToThe52 = FpUtils.scalb(1.0, 52); // 2^52
52
53 double [][] testCases = {
54 {0.0, 0.0},
55 {Double.MIN_VALUE, 0.0},
56 {FpUtils.nextDown(DoubleConsts.MIN_NORMAL), 0.0},
57 {DoubleConsts.MIN_NORMAL, 0.0},
58
59 {0.2, 0.0},
60
61 {FpUtils.nextDown(0.5), 0.0},
62 { 0.5, 0.0},
63 { FpUtils.nextUp(0.5), 1.0},
64
65 {0.7, 1.0},
66 {FpUtils.nextDown(1.0), 1.0},
67 { 1.0, 1.0},
68 { FpUtils.nextUp(1.0), 1.0},
69
70 {FpUtils.nextDown(1.5), 1.0},
71 { 1.5, 2.0},
72 { FpUtils.nextUp(1.5), 2.0},
73
74 {4.2, 4.0},
75 {4.5, 4.0},
76 {4.7, 5.0},
77
78 {7.5, 8.0},
79 {7.2, 7.0},
80 {7.7, 8.0},
81
82 {150000.75, 150001.0},
83 {300000.5, 300000.0},
84 {FpUtils.nextUp(300000.5), 300001.0},
85 {FpUtils.nextDown(300000.75), 300001.0},
86 {300000.75, 300001.0},
87 {FpUtils.nextUp(300000.75), 300001.0},
88 {300000.99, 300001.0},
89 {262144.75, 262145.0}, //(2^18 ) + 0.75
90 {499998.75, 499999.0},
91 {524287.75, 524288.0}, //(2^19 -1) + 0.75
92 {524288.75, 524289.0},
93
94 {FpUtils.nextDown(twoToThe52), twoToThe52},
95 {twoToThe52, twoToThe52},
96 {FpUtils.nextUp(twoToThe52), FpUtils.nextUp(twoToThe52)},
97
98 {Double.MAX_VALUE, Double.MAX_VALUE},
99 {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},
100 {Double.NaN, Double.NaN}
101
102 };
103
104
105 for(int i = 0; i < testCases.length; i++) {
106 failures += testRintCase(testCases[i][0], testCases[i][1]);
107 }
108
109 // Test values throughout exponent range
110 for(double d = Double.MIN_VALUE;
111 d < Double.POSITIVE_INFINITY; d *= 2) {
112 failures += testRintCase(d, ((d<=0.5)?0.0:d));
113 }
114
115 if (failures > 0) {
116 System.err.println("Testing {Math, StrictMath}.rint incurred "
117 + failures + " failures.");
118 throw new RuntimeException();
119 }
120 }
121
122}