From a262aaba15effce48fdba95910bef367f89cafca Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Tue, 17 Jan 2017 01:32:05 -0800 Subject: SI-10148 Follow Java for float literals Use `Float.parseFloat` instead of converting from Double. Error when a value rounds to zero. --- .../scala/tools/nsc/ast/parser/Parsers.scala | 4 +-- .../scala/tools/nsc/ast/parser/Scanners.scala | 34 ++++++++++++++++++---- test/files/neg/literals.check | 14 ++++++++- test/files/neg/literals.scala | 13 ++++++++- test/files/run/literals.scala | 5 ++++ 5 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index cf66e0a7dc..d7d0f01741 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -1263,8 +1263,8 @@ self => case CHARLIT => in.charVal case INTLIT => in.intVal(isNegated).toInt case LONGLIT => in.intVal(isNegated) - case FLOATLIT => in.floatVal(isNegated).toFloat - case DOUBLELIT => in.floatVal(isNegated) + case FLOATLIT => in.floatVal(isNegated) + case DOUBLELIT => in.doubleVal(isNegated) case STRINGLIT | STRINGPART => in.strVal.intern() case TRUE => true case FALSE => false diff --git a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala index a8cc7f91c2..ccd0d4a9c9 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala @@ -972,23 +972,45 @@ trait Scanners extends ScannersCommon { def intVal: Long = intVal(negated = false) - /** Convert current strVal, base to double value + /** Convert current strVal, base to float value. */ - def floatVal(negated: Boolean): Double = { - val limit: Double = if (token == DOUBLELIT) Double.MaxValue else Float.MaxValue + def floatVal(negated: Boolean): Float = { try { - val value: Double = java.lang.Double.valueOf(strVal).doubleValue() - if (value > limit) + val value: Float = java.lang.Float.parseFloat(strVal) + if (value > Float.MaxValue) syntaxError("floating point number too large") + val zeroly = "0.fF" + if (value == 0.0f && strVal.exists(c => !zeroly.contains(c))) + syntaxError("floating point number too small") if (negated) -value else value } catch { case _: NumberFormatException => syntaxError("malformed floating point number") + 0.0f + } + } + + def floatVal: Float = floatVal(negated = false) + + /** Convert current strVal, base to double value. + */ + def doubleVal(negated: Boolean): Double = { + try { + val value: Double = java.lang.Double.parseDouble(strVal) + if (value > Double.MaxValue) + syntaxError("double precision floating point number too large") + val zeroly = "0.dD" + if (value == 0.0d && strVal.exists(c => !zeroly.contains(c))) + syntaxError("double precision floating point number too small") + if (negated) -value else value + } catch { + case _: NumberFormatException => + syntaxError("malformed double precision floating point number") 0.0 } } - def floatVal: Double = floatVal(negated = false) + def doubleVal: Double = doubleVal(negated = false) def checkNoLetter(): Unit = { if (isIdentifierPart(ch) && ch >= ' ') diff --git a/test/files/neg/literals.check b/test/files/neg/literals.check index 148a9346c5..79b6d47782 100644 --- a/test/files/neg/literals.check +++ b/test/files/neg/literals.check @@ -19,6 +19,18 @@ literals.scala:23: error: missing integer number literals.scala:27: error: Decimal integer literals may not have a leading zero. (Octal syntax is obsolete.) def tooManyZeros: Int = 00 // line 26: no leading zero ^ +literals.scala:40: error: floating point number too small + def tooTiny: Float = { 0.7e-45f } // floating point number too small + ^ +literals.scala:42: error: double precision floating point number too small + def twoTiny: Double = { 2.0e-324 } // double precision floating point number too small + ^ +literals.scala:44: error: floating point number too large + def tooHuge: Float = { 3.4028236E38f } // floating point number too large + ^ +literals.scala:46: error: double precision floating point number too large + def twoHuge: Double = { 1.7976931348623159e308 } // double precision floating point number too large + ^ literals.scala:14: error: identifier expected but '}' found. def orphanDot: Int = { 9. } // line 12: ident expected ^ @@ -37,4 +49,4 @@ literals.scala:29: error: ';' expected but 'def' found. literals.scala:33: error: identifier expected but 'def' found. def zeroOfNineDot: Int = 09. // line 32: malformed integer, ident expected ^ -13 errors found +17 errors found diff --git a/test/files/neg/literals.scala b/test/files/neg/literals.scala index 3df7f0b408..22d5d9acd1 100644 --- a/test/files/neg/literals.scala +++ b/test/files/neg/literals.scala @@ -1,6 +1,6 @@ /* This took me literally all day. -*/ + */ trait RejectedLiterals { def missingHex: Int = { 0x } // line 4: was: not reported, taken as zero @@ -34,3 +34,14 @@ trait Braceless { def noHexFloat: Double = 0x1.2 // line 34: ';' expected but double literal found. } + +trait MoreSadness { + + def tooTiny: Float = { 0.7e-45f } // floating point number too small + + def twoTiny: Double = { 2.0e-324 } // double precision floating point number too small + + def tooHuge: Float = { 3.4028236E38f } // floating point number too large + + def twoHuge: Double = { 1.7976931348623159e308 } // double precision floating point number too large +} diff --git a/test/files/run/literals.scala b/test/files/run/literals.scala index 13fda05876..25501123b5 100644 --- a/test/files/run/literals.scala +++ b/test/files/run/literals.scala @@ -84,6 +84,10 @@ object Test { check_success("3.14f == 3.14f", 3.14f, 3.14f) check_success("6.022e23f == 6.022e23f", 6.022e23f, 6.022e23f) check_success("09f == 9.0f", 09f, 9.0f) + check_success("1.00000017881393421514957253748434595763683319091796875001f == 1.0000001f", + 1.00000017881393421514957253748434595763683319091796875001f, + 1.0000001f) + check_success("3.4028235E38f == Float.MaxValue", 3.4028235E38f, Float.MaxValue) check_success("1.asInstanceOf[Float] == 1.0", 1.asInstanceOf[Float], 1.0f) check_success("1l.asInstanceOf[Float] == 1.0", 1l.asInstanceOf[Float], 1.0f) @@ -97,6 +101,7 @@ object Test { check_success("3.14 == 3.14", 3.14, 3.14) check_success("1e-9d == 1.0e-9", 1e-9d, 1.0e-9) check_success("1e137 == 1.0e137", 1e137, 1.0e137) + check_success("1.7976931348623157e308d == Double.MaxValue", 1.7976931348623157e308d, Double.MaxValue) check_success("1.asInstanceOf[Double] == 1.0", 1.asInstanceOf[Double], 1.0) check_success("1l.asInstanceOf[Double] == 1.0", 1l.asInstanceOf[Double], 1.0) -- cgit v1.2.3