By Alvin Alexander. Last updated: March 8, 2018
As a brief note, I knew that the sum
function I wrote in my book on Scala and functional programming would return a wrong value if the sum of the integers in a list was greater than Int.MaxValue
, and I was curious how the built-in sum
method on lists handled the same problem. It turns out that it works in the same way.
So, if you need to sum a list of integers that may exceed Int.MaxValue
, you might need to write a sum function that calculates and returns the sum as a Long
. (The same is true for Float
and Double
.)