Skip to content

Commit 7241204

Browse files
Brian Burkhalterjddarcyphohensee
committed
8211936: Better String parsing
Co-authored-by: Joe Darcy <joe.darcy@oracle.com> Co-authored-by: Paul Hohensee <hohensee@amazon.com> Reviewed-by: bpb, darcy
1 parent 52da980 commit 7241204

4 files changed

Lines changed: 470 additions & 48 deletions

File tree

src/java.base/share/classes/java/math/BigDecimal.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -3414,9 +3414,32 @@ public BigInteger toBigIntegerExact() {
34143414
*/
34153415
@Override
34163416
public long longValue(){
3417-
return (intCompact != INFLATED && scale == 0) ?
3418-
intCompact:
3419-
toBigInteger().longValue();
3417+
if (intCompact != INFLATED && scale == 0) {
3418+
return intCompact;
3419+
} else {
3420+
// Fastpath zero and small values
3421+
if (this.signum() == 0 || fractionOnly() ||
3422+
// Fastpath very large-scale values that will result
3423+
// in a truncated value of zero. If the scale is -64
3424+
// or less, there are at least 64 powers of 10 in the
3425+
// value of the numerical result. Since 10 = 2*5, in
3426+
// that case there would also be 64 powers of 2 in the
3427+
// result, meaning all 64 bits of a long will be zero.
3428+
scale <= -64) {
3429+
return 0;
3430+
} else {
3431+
return toBigInteger().longValue();
3432+
}
3433+
}
3434+
}
3435+
3436+
/**
3437+
* Return true if a nonzero BigDecimal has an absolute value less
3438+
* than one; i.e. only has fraction digits.
3439+
*/
3440+
private boolean fractionOnly() {
3441+
assert this.signum() != 0;
3442+
return (this.precision() - this.scale) <= 0;
34203443
}
34213444

34223445
/**
@@ -3434,15 +3457,20 @@ public long longValue(){
34343457
public long longValueExact() {
34353458
if (intCompact != INFLATED && scale == 0)
34363459
return intCompact;
3437-
// If more than 19 digits in integer part it cannot possibly fit
3438-
if ((precision() - scale) > 19) // [OK for negative scale too]
3439-
throw new java.lang.ArithmeticException("Overflow");
3440-
// Fastpath zero and < 1.0 numbers (the latter can be very slow
3441-
// to round if very small)
3460+
3461+
// Fastpath zero
34423462
if (this.signum() == 0)
34433463
return 0;
3444-
if ((this.precision() - this.scale) <= 0)
3464+
3465+
// Fastpath numbers less than 1.0 (the latter can be very slow
3466+
// to round if very small)
3467+
if (fractionOnly())
34453468
throw new ArithmeticException("Rounding necessary");
3469+
3470+
// If more than 19 digits in integer part it cannot possibly fit
3471+
if ((precision() - scale) > 19) // [OK for negative scale too]
3472+
throw new java.lang.ArithmeticException("Overflow");
3473+
34463474
// round to an integer, with Exception if decimal part non-0
34473475
BigDecimal num = this.setScale(0, ROUND_UNNECESSARY);
34483476
if (num.precision() >= 19) // need to check carefully
@@ -3486,7 +3514,7 @@ public static void check(BigDecimal num) {
34863514
public int intValue() {
34873515
return (intCompact != INFLATED && scale == 0) ?
34883516
(int)intCompact :
3489-
toBigInteger().intValue();
3517+
(int)longValue();
34903518
}
34913519

34923520
/**
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2019, 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 8211936
27+
* @summary Tests of BigDecimal.intValueExact
28+
*/
29+
import java.math.*;
30+
import java.util.List;
31+
import java.util.Map;
32+
import static java.util.Map.entry;
33+
34+
public class IntValueExactTests {
35+
public static void main(String... args) {
36+
int failures = 0;
37+
38+
failures += intValueExactSuccessful();
39+
failures += intValueExactExceptional();
40+
41+
if (failures > 0) {
42+
throw new RuntimeException("Incurred " + failures +
43+
" failures while testing intValueExact.");
44+
}
45+
}
46+
47+
private static int simpleIntValueExact(BigDecimal bd) {
48+
return bd.toBigIntegerExact().intValue();
49+
}
50+
51+
private static int intValueExactSuccessful() {
52+
int failures = 0;
53+
54+
// Strings used to create BigDecimal instances on which invoking
55+
// intValueExact() will succeed.
56+
Map<BigDecimal, Integer> successCases =
57+
Map.ofEntries(entry(new BigDecimal("2147483647"), Integer.MAX_VALUE), // 2^31 -1
58+
entry(new BigDecimal("2147483647.0"), Integer.MAX_VALUE),
59+
entry(new BigDecimal("2147483647.00"), Integer.MAX_VALUE),
60+
61+
entry(new BigDecimal("-2147483648"), Integer.MIN_VALUE), // -2^31
62+
entry(new BigDecimal("-2147483648.0"), Integer.MIN_VALUE),
63+
entry(new BigDecimal("-2147483648.00"),Integer.MIN_VALUE),
64+
65+
entry(new BigDecimal("1e0"), 1),
66+
entry(new BigDecimal(BigInteger.ONE, -9), 1_000_000_000),
67+
68+
entry(new BigDecimal("0e13"), 0), // Fast path zero
69+
entry(new BigDecimal("0e32"), 0),
70+
entry(new BigDecimal("0e512"), 0),
71+
72+
entry(new BigDecimal("10.000000000000000000000000000000000"), 10));
73+
74+
for (var testCase : successCases.entrySet()) {
75+
BigDecimal bd = testCase.getKey();
76+
int expected = testCase.getValue();
77+
try {
78+
int intValueExact = bd.intValueExact();
79+
if (expected != intValueExact ||
80+
intValueExact != simpleIntValueExact(bd)) {
81+
failures++;
82+
System.err.println("Unexpected intValueExact result " + intValueExact +
83+
" on " + bd);
84+
}
85+
} catch (Exception e) {
86+
failures++;
87+
System.err.println("Error on " + bd + "\tException message:" + e.getMessage());
88+
}
89+
}
90+
return failures;
91+
}
92+
93+
private static int intValueExactExceptional() {
94+
int failures = 0;
95+
List<BigDecimal> exceptionalCases =
96+
List.of(new BigDecimal("2147483648"), // Integer.MAX_VALUE + 1
97+
new BigDecimal("2147483648.0"),
98+
new BigDecimal("2147483648.00"),
99+
new BigDecimal("-2147483649"), // Integer.MIN_VALUE - 1
100+
new BigDecimal("-2147483649.1"),
101+
new BigDecimal("-2147483649.01"),
102+
103+
new BigDecimal("9999999999999999999999999999999"),
104+
new BigDecimal("10000000000000000000000000000000"),
105+
106+
new BigDecimal("0.99"),
107+
new BigDecimal("0.999999999999999999999"));
108+
109+
for (BigDecimal bd : exceptionalCases) {
110+
try {
111+
int intValueExact = bd.intValueExact();
112+
failures++;
113+
System.err.println("Unexpected non-exceptional intValueExact on " + bd);
114+
} catch (ArithmeticException e) {
115+
// Success;
116+
}
117+
}
118+
return failures;
119+
}
120+
}

0 commit comments

Comments
 (0)