object OverflowCheck {
val LongMax: Long = Long.MaxValue
val LongMin: Long = Long.MinValue
def addingWillOverflow(x: Long, y: Long): Boolean = {
(x > 0 && y > LongMax - x) || (x < 0 && y < LongMin - x)
}
def main(args: Array[String]): Unit = {
val x: Long = 39839299
val y: Long = 1472783642
println(if (addingWillOverflow(x, y)) "true" else "false")
val x2: Long = 9223372036854775807L // Long.MaxValue
val y2: Long = 1
println(if (addingWillOverflow(x2, y2)) "true" else "false")
}
}
/*
run:
false
true
*/