Alex Rivera | Logout

scala won't take a 12-digit integer

Asked 2010-09-05T01:42:13.447
16

ok so I'm just getting started in scala.. ran into a weird problem with a large number.

import Math._
var num:Long=0
num+=600851475
num*=1000
println(num)

that code works fine, yet the following doesn't compile with an error saying the integer is too large.

import Math._
var num:Long=0
num+=600851475000
println(num)

what's up? can scala not handle a 12-digit number? :/

Edit
Report

1 Answer

17

Even though num is declared to be a Long, 600851475000 is read by the compiler to be an Int, which can only handle numbers in [-2^32, 2^32) [-2^31, 2^31). Writing the number as 600851475000L tells the compiler to treat it as a Long, which will handle numbers up to about 18 digits.

answered 2010-09-05T01:52:29.610

Your Answer