author | mgronlun |
Sun, 22 Sep 2019 17:31:57 +0200 (2019-09-22) | |
branch | JEP-349-branch |
changeset 58259 | b6efcf2217f1 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
1826 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
18537
diff
changeset
|
2 |
* Copyright (c) 1998, 2013, 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 |
|
18537
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
26 |
* @bug 4160406 4705734 4707389 6358355 7032154 |
1826 | 27 |
* @summary Tests for Float.parseFloat method |
28 |
*/ |
|
29 |
||
18537
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
30 |
import java.math.BigDecimal; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
31 |
import java.math.BigInteger; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
32 |
|
1826 | 33 |
public class ParseFloat { |
34 |
||
18537
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
35 |
private static final BigDecimal HALF = BigDecimal.valueOf(0.5); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
36 |
|
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
37 |
private static void fail(String val, float n) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
38 |
throw new RuntimeException("Float.parseFloat failed. String:" + |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
39 |
val + " Result:" + n); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
40 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
41 |
|
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
42 |
private static void check(String val) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
43 |
float n = Float.parseFloat(val); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
44 |
boolean isNegativeN = n < 0 || n == 0 && 1/n < 0; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
45 |
float na = Math.abs(n); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
46 |
String s = val.trim().toLowerCase(); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
47 |
switch (s.charAt(s.length() - 1)) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
48 |
case 'd': |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
49 |
case 'f': |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
50 |
s = s.substring(0, s.length() - 1); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
51 |
break; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
52 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
53 |
boolean isNegative = false; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
54 |
if (s.charAt(0) == '+') { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
55 |
s = s.substring(1); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
56 |
} else if (s.charAt(0) == '-') { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
57 |
s = s.substring(1); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
58 |
isNegative = true; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
59 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
60 |
if (s.equals("nan")) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
61 |
if (!Float.isNaN(n)) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
62 |
fail(val, n); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
63 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
64 |
return; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
65 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
66 |
if (Float.isNaN(n)) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
67 |
fail(val, n); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
68 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
69 |
if (isNegativeN != isNegative) |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
70 |
fail(val, n); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
71 |
if (s.equals("infinity")) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
72 |
if (na != Float.POSITIVE_INFINITY) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
73 |
fail(val, n); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
74 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
75 |
return; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
76 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
77 |
BigDecimal bd; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
78 |
if (s.startsWith("0x")) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
79 |
s = s.substring(2); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
80 |
int indP = s.indexOf('p'); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
81 |
long exp = Long.parseLong(s.substring(indP + 1)); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
82 |
int indD = s.indexOf('.'); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
83 |
String significand; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
84 |
if (indD >= 0) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
85 |
significand = s.substring(0, indD) + s.substring(indD + 1, indP); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
86 |
exp -= 4*(indP - indD - 1); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
87 |
} else { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
88 |
significand = s.substring(0, indP); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
89 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
90 |
bd = new BigDecimal(new BigInteger(significand, 16)); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
91 |
if (exp >= 0) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
92 |
bd = bd.multiply(BigDecimal.valueOf(2).pow((int)exp)); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
93 |
} else { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
94 |
bd = bd.divide(BigDecimal.valueOf(2).pow((int)-exp)); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
95 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
96 |
} else { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
97 |
bd = new BigDecimal(s); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
98 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
99 |
BigDecimal l, u; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
100 |
if (Float.isInfinite(na)) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
101 |
l = new BigDecimal(Float.MAX_VALUE).add(new BigDecimal(Math.ulp(Float.MAX_VALUE)).multiply(HALF)); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
102 |
u = null; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
103 |
} else { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
104 |
l = new BigDecimal(na).subtract(new BigDecimal(Math.ulp(-Math.nextUp(-na))).multiply(HALF)); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
105 |
u = new BigDecimal(na).add(new BigDecimal(Math.ulp(n)).multiply(HALF)); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
106 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
107 |
int cmpL = bd.compareTo(l); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
108 |
int cmpU = u != null ? bd.compareTo(u) : -1; |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
109 |
if ((Float.floatToIntBits(n) & 1) != 0) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
110 |
if (cmpL <= 0 || cmpU >= 0) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
111 |
fail(val, n); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
112 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
113 |
} else { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
114 |
if (cmpL < 0 || cmpU > 0) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
115 |
fail(val, n); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
116 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
117 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
118 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
119 |
|
1826 | 120 |
private static void check(String val, float expected) { |
121 |
float n = Float.parseFloat(val); |
|
122 |
if (n != expected) |
|
18537
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
123 |
fail(val, n); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
124 |
check(val); |
1826 | 125 |
} |
126 |
||
127 |
private static void rudimentaryTest() { |
|
128 |
check(new String(""+Float.MIN_VALUE), Float.MIN_VALUE); |
|
129 |
check(new String(""+Float.MAX_VALUE), Float.MAX_VALUE); |
|
130 |
||
131 |
check("10", (float) 10.0); |
|
132 |
check("10.0", (float) 10.0); |
|
133 |
check("10.01", (float) 10.01); |
|
134 |
||
135 |
check("-10", (float) -10.0); |
|
136 |
check("-10.00", (float) -10.0); |
|
137 |
check("-10.01", (float) -10.01); |
|
18537
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
138 |
|
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
139 |
// bug 6358355 |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
140 |
check("144115196665790480", 0x1.000002p57f); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
141 |
check("144115196665790481", 0x1.000002p57f); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
142 |
check("0.050000002607703203", 0.05f); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
143 |
check("0.050000002607703204", 0.05f); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
144 |
check("0.050000002607703205", 0.05f); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
145 |
check("0.050000002607703206", 0.05f); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
146 |
check("0.050000002607703207", 0.05f); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
147 |
check("0.050000002607703208", 0.05f); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
148 |
check("0.050000002607703209", 0.050000004f); |
1826 | 149 |
} |
150 |
||
151 |
static String badStrings[] = { |
|
152 |
"", |
|
153 |
"+", |
|
154 |
"-", |
|
155 |
"+e", |
|
156 |
"-e", |
|
157 |
"+e170", |
|
158 |
"-e170", |
|
159 |
||
160 |
// Make sure intermediate white space is not deleted. |
|
161 |
"1234 e10", |
|
162 |
"-1234 e10", |
|
163 |
||
164 |
// Control characters in the interior of a string are not legal |
|
165 |
"1\u0007e1", |
|
166 |
"1e\u00071", |
|
167 |
||
168 |
// NaN and infinity can't have trailing type suffices or exponents |
|
169 |
"NaNf", |
|
170 |
"NaNF", |
|
171 |
"NaNd", |
|
172 |
"NaND", |
|
173 |
"-NaNf", |
|
174 |
"-NaNF", |
|
175 |
"-NaNd", |
|
176 |
"-NaND", |
|
177 |
"+NaNf", |
|
178 |
"+NaNF", |
|
179 |
"+NaNd", |
|
180 |
"+NaND", |
|
181 |
"Infinityf", |
|
182 |
"InfinityF", |
|
183 |
"Infinityd", |
|
184 |
"InfinityD", |
|
185 |
"-Infinityf", |
|
186 |
"-InfinityF", |
|
187 |
"-Infinityd", |
|
188 |
"-InfinityD", |
|
189 |
"+Infinityf", |
|
190 |
"+InfinityF", |
|
191 |
"+Infinityd", |
|
192 |
"+InfinityD", |
|
193 |
||
194 |
"NaNe10", |
|
195 |
"-NaNe10", |
|
196 |
"+NaNe10", |
|
197 |
"Infinitye10", |
|
198 |
"-Infinitye10", |
|
199 |
"+Infinitye10", |
|
200 |
||
201 |
// Non-ASCII digits are not recognized |
|
202 |
"\u0661e\u0661", // 1e1 in Arabic-Indic digits |
|
203 |
"\u06F1e\u06F1", // 1e1 in Extended Arabic-Indic digits |
|
204 |
"\u0967e\u0967" // 1e1 in Devanagari digits |
|
205 |
}; |
|
206 |
||
207 |
static String goodStrings[] = { |
|
208 |
"NaN", |
|
209 |
"+NaN", |
|
210 |
"-NaN", |
|
211 |
"Infinity", |
|
212 |
"+Infinity", |
|
213 |
"-Infinity", |
|
214 |
"1.1e-23f", |
|
215 |
".1e-23f", |
|
216 |
"1e-23", |
|
217 |
"1f", |
|
218 |
"1", |
|
219 |
"2", |
|
220 |
"1234", |
|
221 |
"-1234", |
|
222 |
"+1234", |
|
223 |
"2147483647", // Integer.MAX_VALUE |
|
224 |
"2147483648", |
|
225 |
"-2147483648", // Integer.MIN_VALUE |
|
226 |
"-2147483649", |
|
227 |
||
228 |
"16777215", |
|
229 |
"16777216", // 2^24 |
|
230 |
"16777217", |
|
231 |
||
232 |
"-16777215", |
|
233 |
"-16777216", // -2^24 |
|
234 |
"-16777217", |
|
235 |
||
236 |
"9007199254740991", |
|
237 |
"9007199254740992", // 2^53 |
|
238 |
"9007199254740993", |
|
239 |
||
240 |
"-9007199254740991", |
|
241 |
"-9007199254740992", // -2^53 |
|
242 |
"-9007199254740993", |
|
243 |
||
244 |
"9223372036854775807", |
|
245 |
"9223372036854775808", // Long.MAX_VALUE |
|
246 |
"9223372036854775809", |
|
247 |
||
248 |
"-9223372036854775808", |
|
249 |
"-9223372036854775809", // Long.MIN_VALUE |
|
250 |
"-9223372036854775810" |
|
251 |
}; |
|
252 |
||
253 |
static String paddedBadStrings[]; |
|
254 |
static String paddedGoodStrings[]; |
|
255 |
static { |
|
256 |
String pad = " \t\n\r\f\u0001\u000b\u001f"; |
|
257 |
paddedBadStrings = new String[badStrings.length]; |
|
258 |
for(int i = 0 ; i < badStrings.length; i++) |
|
259 |
paddedBadStrings[i] = pad + badStrings[i] + pad; |
|
260 |
||
261 |
paddedGoodStrings = new String[goodStrings.length]; |
|
262 |
for(int i = 0 ; i < goodStrings.length; i++) |
|
263 |
paddedGoodStrings[i] = pad + goodStrings[i] + pad; |
|
264 |
||
265 |
} |
|
266 |
||
267 |
/* |
|
268 |
* Throws an exception if <code>Input</code> is |
|
269 |
* <code>exceptionalInput</code> and {@link Float.parseFloat |
|
270 |
* parseFloat} does <em>not</em> throw an exception or if |
|
271 |
* <code>Input</code> is not <code>exceptionalInput</code> and |
|
272 |
* <code>parseFloat</code> throws an exception. This method does |
|
273 |
* not attempt to test whether the string is converted to the |
|
274 |
* proper value; just whether the input is accepted appropriately |
|
275 |
* or not. |
|
276 |
*/ |
|
277 |
private static void testParsing(String [] input, |
|
278 |
boolean exceptionalInput) { |
|
279 |
for(int i = 0; i < input.length; i++) { |
|
280 |
double d; |
|
281 |
||
282 |
try { |
|
283 |
d = Float.parseFloat(input[i]); |
|
18537
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
284 |
check(input[i]); |
1826 | 285 |
} |
286 |
catch (NumberFormatException e) { |
|
287 |
if (! exceptionalInput) { |
|
288 |
throw new RuntimeException("Float.parseFloat rejected " + |
|
289 |
"good string `" + input[i] + |
|
290 |
"'."); |
|
291 |
} |
|
292 |
break; |
|
293 |
} |
|
294 |
if (exceptionalInput) { |
|
295 |
throw new RuntimeException("Float.parseFloat accepted " + |
|
296 |
"bad string `" + input[i] + |
|
297 |
"'."); |
|
298 |
} |
|
299 |
} |
|
300 |
} |
|
301 |
||
18537
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
302 |
/** |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
303 |
* For each power of two, test at boundaries of |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
304 |
* region that should convert to that value. |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
305 |
*/ |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
306 |
private static void testPowers() { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
307 |
for(int i = -149; i <= +127; i++) { |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
308 |
float f = Math.scalb(1.0f, i); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
309 |
BigDecimal f_BD = new BigDecimal(f); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
310 |
|
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
311 |
BigDecimal lowerBound = f_BD.subtract(new BigDecimal(Math.ulp(-Math.nextUp(-f))).multiply(HALF)); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
312 |
BigDecimal upperBound = f_BD.add(new BigDecimal(Math.ulp(f)).multiply(HALF)); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
313 |
|
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
314 |
check(lowerBound.toString()); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
315 |
check(upperBound.toString()); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
316 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
317 |
check(new BigDecimal(Float.MAX_VALUE).add(new BigDecimal(Math.ulp(Float.MAX_VALUE)).multiply(HALF)).toString()); |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
318 |
} |
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
319 |
|
1826 | 320 |
public static void main(String[] args) throws Exception { |
321 |
rudimentaryTest(); |
|
322 |
||
323 |
testParsing(goodStrings, false); |
|
324 |
testParsing(paddedGoodStrings, false); |
|
325 |
testParsing(badStrings, true); |
|
326 |
testParsing(paddedBadStrings, true); |
|
18537
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
327 |
|
2ae90e54f001
7192954: Fix Float.parseFloat to round correctly and preserve monotonicity.
bpb
parents:
5506
diff
changeset
|
328 |
testPowers(); |
1826 | 329 |
} |
330 |
} |