Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Scala 2.11.2 give me compilation error for floating point number in scientific notation?

I've just recently updated a Scala project from 2.10 to 2.11.2.

For the following code:

 if( x < 1.e-150 ) // conditional ops... 

I'm getting the error

e is not a member of Int

Previously scientific notion has worked ok. I suspect it is not a 2.11 thing but more likely some weirdness with the upgrade which was for most part just a case of updating the sbt file to have:

scalaVersion := "2.11.2"

where it was previously:

scalaVersion := "2.10.3"

I can't think of what might be causing this. All I know is that it was working fine under 2.10.

Has anyone seen this issue before or can suggest a fix? ( or new line of enquiry!)

I'm using JDK 1.7.0_21 and sbt.version=0.13.5.

Any help or ideas appreciated.

like image 554
ZenMasterZed Avatar asked Sep 12 '14 10:09

ZenMasterZed


1 Answers

The syntax for scientific notation is Scala is a floating point number followed by e (or E) and the exponent. The issue you're seeing is a change in what counts as a floating point number, and doesn't properly have anything to do with the syntax for scientific notation (although that does contribute to the confusingness of the error message).

You can confirm this by firing up a 2.10.4 REPL with -deprecation turned on:

scala> val x = 1.
<console>:1: warning: This lexical syntax is deprecated.  From scala 2.11, a dot
         will only be considered part of a number if it is immediately followed
         by a digit.
       val x = 1.
               ^

And sure enough, in 2.11 this just won't compile at all.

You can accomplish exactly the same thing by writing either 1e-150 or 1.0e-150, both of which will work in either 2.10 or 2.11.

like image 128
Travis Brown Avatar answered Oct 13 '22 04:10

Travis Brown