Scala - Sum of positive Ints being negative - Overflow Exception? -
i had hard time finding bug being caused due large sum of positive ints being greater integer.max_value.
my question is: why:
val a:int = integer.max_value + 2 -> = -2147483648 returns negative number, opposed giving overflow exception doing:
val b:int =999999999999999999 -> error: integer number large
why not throwing exception standard?
adding checks costs performance , code rely on how overflows work.
if don't want overflow java 8 has solution:
math.addexact(left, right); https://docs.oracle.com/javase/8/docs/api/java/lang/math.html#addexact-int-int-
Comments
Post a Comment