ICU-13701 Use exact string-to-double conversion in Java.

This commit is contained in:
Shane Carr 2019-01-10 21:23:37 -08:00 committed by Shane F. Carr
parent 768b577e6a
commit 01bd502a95
2 changed files with 4 additions and 41 deletions

View file

@ -657,45 +657,9 @@ public abstract class DecimalQuantity_AbstractBCD implements DecimalQuantity {
return isNegative() ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
}
// TODO: Do like in C++ and use a library function to perform this conversion?
// This code is not as hot in Java because .parse() returns a BigDecimal, not a double.
long tempLong = 0L;
int lostDigits = precision - Math.min(precision, 17);
for (int shift = precision - 1; shift >= lostDigits; shift--) {
tempLong = tempLong * 10 + getDigitPos(shift);
}
double result = tempLong;
int _scale = scale + lostDigits;
if (_scale >= 0) {
// 1e22 is the largest exact double.
int i = _scale;
for (; i >= 22; i -= 22) {
result *= 1e22;
if (Double.isInfinite(result)) {
// Further multiplications will not be productive.
i = 0;
break;
}
}
result *= DOUBLE_MULTIPLIERS[i];
} else {
// 1e22 is the largest exact double.
int i = _scale;
for (; i <= -22; i += 22) {
result /= 1e22;
if (result == 0.0) {
// Further divisions will not be productive.
i = 0;
break;
}
}
result /= DOUBLE_MULTIPLIERS[-i];
}
if (isNegative()) {
result = -result;
}
return result;
StringBuilder sb = new StringBuilder();
toScientificString(sb);
return Double.valueOf(sb.toString());
}
@Override

View file

@ -492,8 +492,7 @@ public class DecimalQuantityTest extends TestFmwk {
Object[][] cases = new Object[][] {
{ "0", 0.0 },
{ "514.23", 514.23 },
// NOTE: This does not currently pass in Java. See DecimalFormat_AbstractBCD#toDecimal.
// { "-3.142E-271", -3.142e-271 }
{ "-3.142E-271", -3.142e-271 }
};
for (Object[] cas : cases) {