author | jrose |
Sat, 05 Oct 2013 05:30:38 -0700 | |
changeset 20528 | 0b1e2130d3f7 |
parent 10608 | 7cfca36fc79b |
child 26197 | 1bb6b68b87cd |
permissions | -rw-r--r-- |
1826 | 1 |
/* |
10608 | 2 |
* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. |
1826 | 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 |
* |
|
5506 | 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. |
|
1826 | 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; |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
49 |
double twoToThe52 = Math.scalb(1.0, 52); // 2^52 |
1826 | 50 |
|
51 |
double [][] testCases = { |
|
52 |
{0.0, 0.0}, |
|
53 |
{Double.MIN_VALUE, 0.0}, |
|
10608 | 54 |
{Math.nextDown(DoubleConsts.MIN_NORMAL), 0.0}, |
1826 | 55 |
{DoubleConsts.MIN_NORMAL, 0.0}, |
56 |
||
57 |
{0.2, 0.0}, |
|
58 |
||
10608 | 59 |
{Math.nextDown(0.5), 0.0}, |
60 |
{ 0.5, 0.0}, |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
61 |
{ Math.nextUp(0.5), 1.0}, |
1826 | 62 |
|
63 |
{0.7, 1.0}, |
|
10608 | 64 |
{Math.nextDown(1.0), 1.0}, |
65 |
{ 1.0, 1.0}, |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
66 |
{ Math.nextUp(1.0), 1.0}, |
1826 | 67 |
|
10608 | 68 |
{Math.nextDown(1.5), 1.0}, |
69 |
{ 1.5, 2.0}, |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
70 |
{ Math.nextUp(1.5), 2.0}, |
1826 | 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}, |
|
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
82 |
{Math.nextUp(300000.5), 300001.0}, |
10608 | 83 |
{Math.nextDown(300000.75), 300001.0}, |
1826 | 84 |
{300000.75, 300001.0}, |
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
85 |
{Math.nextUp(300000.75), 300001.0}, |
1826 | 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 |
||
10608 | 92 |
{Math.nextDown(twoToThe52), twoToThe52}, |
1826 | 93 |
{twoToThe52, twoToThe52}, |
10598
efd29b4b3e67
7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents:
5506
diff
changeset
|
94 |
{Math.nextUp(twoToThe52), Math.nextUp(twoToThe52)}, |
1826 | 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 |
} |