No exception thrown for integer overflow in Scala?

386 views
Skip to first unread message

Caspar Abbey

unread,
Mar 9, 2016, 12:32:17 PM3/9/16
to scala-user
I'm reading the book Programming in Scala (2nd Edition)

As a transition learner from C# to Scala, I know that it'd throw an exception when integer overflow occurs in C#. Because the default behavior of compiler will check the integer overflow with keyword checked. if you want disable the exception, use keyword unchecked explicitly.

But in this book, I never find the discussion about the integer overflow. Does nothing happen when integer overflow occurs except the value should be a negative, doesn't it? Is it the default behavior in Scala? Should I look for more details in Java?

Best regards.

Dennis Haupt

unread,
Mar 9, 2016, 1:02:42 PM3/9/16
to Caspar Abbey, scala-user
there simply is no error thrown, neither in scala nor in java. if you want that, you have to implement your own int class.
also, i don't know about c#, but java can divide by zero if its a double or float and the result is infinity.
 
Gesendet: Mittwoch, 09. März 2016 um 17:30 Uhr
Von: "Caspar Abbey" <korby...@gmail.com>
An: scala-user <scala...@googlegroups.com>
Betreff: [scala-user] No exception thrown for integer overflow in Scala?
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon Ochsenreither

unread,
Mar 13, 2016, 6:18:29 PM3/13/16
to scala-user
Hi Caspar,


As a transition learner from C# to Scala, I know that it'd throw an exception when integer overflow occurs in C#. Because the default behavior of compiler will check the integer overflow with keyword checked. if you want disable the exception, use keyword unchecked explicitly.

Neither C#, nor Java or Scala throw an exception on integer overflow.
C# has the keyword checked to enable it, but it's off by default. I think due to the weird (~broken) scoping behavior of checked, it isn't used much in C# and hasn't been adopted in java/Scala either.
 
But in this book, I never find the discussion about the integer overflow. Does nothing happen when integer overflow occurs except the value should be a negative, doesn't it? Is it the default behavior in Scala? Should I look for more details in Java?

You can use the operations in https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html like addExact, subtractExact, multiplyExact to get exceptions on overflow in Java/Scala.

Oliver Ruebenacker

unread,
Mar 14, 2016, 9:16:24 AM3/14/16
to Simon Ochsenreither, scala-user

     Hello,

  Adding or subtracting integers native to the hardware is a very cheap and often used operation. Checking for integer overflow is quite expensive in comparison and therefore rarely used.

  Operations on Doubles, on the other hand, are comparatively expensive to begin with and adding overflow checks on the hardware level is relatively cheap.

     Best, Oliver

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Rüdiger Klaehn

unread,
Mar 14, 2016, 9:25:19 AM3/14/16
to Caspar Abbey, scala-user
Hi Caspar,

As other people have said, scala (just like java) does not do integer
overflow checks (By the way: for C# the default is unchecked as well).

A fast method of detecting integer overflows is implemented as a macro
in the spire math library:
https://github.com/non/spire/blob/master/macros/src/main/scala/spire/macros/Checked.scala

Spire also contains fast, arbitrary precision integers for when you do
not want overflows to occur at all.

Hth,

Rüdiger
Reply all
Reply to author
Forward
0 new messages