Fix Float.parse and Double.parse for some values

This commit is contained in:
Alexey Andreev 2020-10-07 11:59:33 +03:00
parent 1785d6ad9e
commit 74cc1d8d7d
4 changed files with 9 additions and 4 deletions

View File

@ -121,7 +121,7 @@ public class TDouble extends TNumber implements TComparable<TDouble> {
if (c < '0' || c > '9') {
break;
}
if (mantissa < 1E17) {
if (mantissa < TLong.MAX_VALUE / 10 - 9) {
mantissa = mantissa * 10 + (c - '0');
} else {
++exp;
@ -136,7 +136,7 @@ public class TDouble extends TNumber implements TComparable<TDouble> {
if (c < '0' || c > '9') {
break;
}
if (mantissa < 1E17) {
if (mantissa < TLong.MAX_VALUE / 10 - 9) {
mantissa = mantissa * 10 + (c - '0');
--exp;
}

View File

@ -167,7 +167,7 @@ public class TFloat extends TNumber implements TComparable<TFloat> {
if (c < '0' || c > '9') {
break;
}
if (mantissa < 1E8) {
if (mantissa < (TInteger.MAX_VALUE / 10) - 9) {
mantissa = mantissa * 10 + (c - '0');
} else {
++exp;
@ -183,7 +183,7 @@ public class TFloat extends TNumber implements TComparable<TFloat> {
if (c < '0' || c > '9') {
break;
}
if (mantissa < 1E38) {
if (mantissa < (TInteger.MAX_VALUE / 10) - 9) {
mantissa = mantissa * 10 + (c - '0');
--exp;
}

View File

@ -49,6 +49,9 @@ public class DoubleTest {
assertEquals(0, Double.parseDouble("23E-8000"), 1E-12);
assertEquals(0, Double.parseDouble("00000"), 1E-12);
assertEquals(0, Double.parseDouble("00000.0000"), 1E-12);
assertEquals(4499999999999888888888888.0, Double.parseDouble("4499999999999888888888888"), 1E9);
assertEquals(0.4499999999999888888888888, Double.parseDouble("0.4499999999999888888888888"), 1E-15);
}
@Test

View File

@ -50,6 +50,8 @@ public class FloatTest {
assertEquals(0, Float.parseFloat("23E-8000"), 1E-12F);
assertEquals(0, Float.parseFloat("00000"), 1E-12F);
assertEquals(0, Float.parseFloat("00000.0000"), 1E-12F);
assertEquals(4499999285F, Float.parseFloat("4499999285"), 100F);
assertEquals(0.4499999285F, Float.parseFloat("0.4499999285"), 1E-9F);
}
@Test