I'm running into a strange issue when trying to sum a list of doubles that are contained in different instances using foldLeft
. Upon investigation, it seems that even when working with a list of simple doubles, the issue persists:
val listOfDoubles = List(4.0, 100.0, 1.0, 0.6, 8.58, 80.0, 22.33, 179.99, 8.3, 59.0, 0.6)
listOfDoubles.foldLeft(0.0) ((elem, res) => res + elem) // gives 464.40000000000003 instead of 464.40
What am I doing wrong here?
NOTE: foldLeft
here is necessary as what I'm trying to achieve is a sum of doubles contained in different instances of a case class SomeClass(value: Double)
, unless, of course, there is another method to go about this.