Support '.<digits>' floating literals in Double.parseDouble

See #327
This commit is contained in:
Alexey Andreev 2018-02-16 23:17:17 +03:00
parent 0485930c1f
commit 52a23fcadd
4 changed files with 57 additions and 46 deletions

View File

@ -83,11 +83,13 @@ public class TDouble extends TNumber implements TComparable<TDouble> {
++index;
}
char c = string.charAt(index);
long mantissa = 0;
int exp = 0;
if (c != '.') {
if (c < '0' || c > '9') {
throw new TNumberFormatException();
}
long mantissa = 0;
int exp = 0;
while (string.charAt(index) == '0') {
if (++index == string.length()) {
return 0;
@ -105,6 +107,7 @@ public class TDouble extends TNumber implements TComparable<TDouble> {
}
++index;
}
}
if (index < string.length() && string.charAt(index) == '.') {
++index;
boolean hasOneDigit = false;

View File

@ -117,11 +117,14 @@ public class TFloat extends TNumber implements TComparable<TFloat> {
++index;
}
char c = string.charAt(index);
int mantissa = 0;
int exp = 0;
if (c != '.') {
if (c < '0' || c > '9') {
throw new TNumberFormatException();
}
int mantissa = 0;
int exp = 0;
while (string.charAt(index) == '0') {
if (++index == string.length()) {
return 0;
@ -139,6 +142,8 @@ public class TFloat extends TNumber implements TComparable<TFloat> {
}
++index;
}
}
if (index < string.length() && string.charAt(index) == '.') {
++index;
boolean hasOneDigit = false;

View File

@ -34,6 +34,7 @@ public class DoubleTest {
assertEquals(23, Double.parseDouble("00230000e-4"), 1E-12);
assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12);
assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12);
assertEquals(0.1, Double.parseDouble(".1"), 0.001);
}
@Test

View File

@ -25,28 +25,30 @@ import org.teavm.junit.TeaVMTestRunner;
public class FloatTest {
@Test
public void parsed() {
assertEquals(23, Double.parseDouble("23"), 1E-12);
assertEquals(23, Double.parseDouble("23.0"), 1E-12);
assertEquals(23, Double.parseDouble("23E0"), 1E-12);
assertEquals(23, Double.parseDouble("2.30000E1"), 1E-12);
assertEquals(23, Double.parseDouble("0.23E2"), 1E-12);
assertEquals(23, Double.parseDouble("0.000023E6"), 1E-12);
assertEquals(23, Double.parseDouble("00230000e-4"), 1E-12);
assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12);
assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12);
assertEquals(23, Float.parseFloat("23"), 1E-12F);
assertEquals(23, Float.parseFloat("23.0"), 1E-12F);
assertEquals(23, Float.parseFloat("23E0"), 1E-12F);
assertEquals(23, Float.parseFloat("2.30000E1"), 1E-12F);
assertEquals(23, Float.parseFloat("0.23E2"), 1E-12F);
assertEquals(23, Float.parseFloat("0.000023E6"), 1E-12F);
assertEquals(23, Float.parseFloat("00230000e-4"), 1E-12F);
assertEquals(23, Float.parseFloat("2300000000000000000000e-20"), 1E-12F);
assertEquals(23, Float.parseFloat("2300000000000000000000e-20"), 1E-12F);
assertEquals(23, Float.parseFloat("2300000000000000000000e-20"), 1E-12F);
assertEquals(0.1F, Float.parseFloat("0.1"), 0.001F);
}
@Test
public void negativeParsed() {
assertEquals(-23, Double.parseDouble("-23"), 1E-12);
assertEquals(-23, Float.parseFloat("-23"), 1E-12F);
}
@Test
public void zeroParsed() {
assertEquals(0, Double.parseDouble("0.0"), 1E-12);
assertEquals(0, Double.parseDouble("23E-8000"), 1E-12);
assertEquals(0, Double.parseDouble("00000"), 1E-12);
assertEquals(0, Double.parseDouble("00000.0000"), 1E-12);
assertEquals(0, Float.parseFloat("0.0"), 1E-12F);
assertEquals(0, Float.parseFloat("23E-8000"), 1E-12F);
assertEquals(0, Float.parseFloat("00000"), 1E-12F);
assertEquals(0, Float.parseFloat("00000.0000"), 1E-12F);
}
@Test