What am I doing wrong here?
Using doubles when you need this kind of precision. It's not a problem with
foldLeft. You're suffering from floating point rounding error. The problem is that some numbers need very long (or infinite) representations in binary, which means that they need to be truncated in binary form. And when converted back to decimal, that binary rounding error surfaces.
BigDecimal instead, as it is designed to for arbitrary precision.
scala> val list: List[BigDecimal] = List(4.0, 100.0, 1.0, 0.6, 8.58, 80.0, 22.33, 179.99, 8.3, 59.0, 0.6) list: List[BigDecimal] = List(4.0, 100.0, 1.0, 0.6, 8.58, 80.0, 22.33, 179.99, 8.3, 59.0, 0.6) scala> list.foldLeft(BigDecimal(0.0))(_ + _) res3: scala.math.BigDecimal = 464.40
scala> list.sum res4: BigDecimal = 464.40
- foldLeft on list of Doubles returns strange value
- How to pass the initial value to foldLeft from a filtered List with function chaining?
- foldLeft on List of Tuples: strange unexpected results
- Map function returns value inside Iterable List ? Scala
- How can I override a typesafe config list value on the command line?
- How can I find the index of the maximum value in a List in Scala?
- Applying an argument list to curried function using foldLeft in Scala
- Add to list if value is not null
- scala main returns unit. How to set program's return value
- Spark ML VectorAssembler returns strange output
- Scala error: value sort is not a member of List
- Method that returns List of size n in Shapeless
- Appending to Map with value as a list
- Scala: idiomatic way to merge list of maps with the greatest value of each key?
- How to find value closest to List of values?
- How to sum values and group them by a key value in Scala's List of Map?
- Reduce / fold over list of monoid but reducer returns Either
- calling reduce on list of akka http routes yields compilation error (no implicit value for parameter join)
- Scala map list element to a value calculated from previous elements
- Generic function that accepts two monadic values and returns a single monadic value
- In scala, how do you transform a list of futures into a future that returns the first successful future?
- Scala - Using Filter to see if the value in one list matches the value in the same index in another list
- Filtering a list of (A, Option[B]) and extracting value from Option
- Retain MAX value of Aerospike CDT List
- Apply list of functions to value using Scala Cats
- How to find the position of a value with foldLeft from a List?
- Min value of Integer List when it can be empty in scala
- Json.asString returns None even though Json.toString returns the correct value
- How Scala Array apply method returns the value at the index when the implementation is simply throw new error
- How set default list value if system variable is not present in typesafe configuration?
More Query from same tag
- How to change RowMatrix into Array in Spark or export it as a CSV?
- Pass variable as attribute to XML body
- How to run scalatest specs with two different dependency injection containers?
- Scala Breeze (a library for numerical processing); How to convert a DenseMatrix of Int's to a DenseMatrix of Doubles?
- inferSchema=true isn't working for csv file reading n Spark Structured Streaming
- What does this scala expression mean? val h3 : Holder[Person] = h2
- Scala List object methods implementation
- Does extending a class in scala inherits auxilary constuctor also?
- Scala: Why can't I increment the int like this?
- Error: md5sum: stat '>': No such file or directory
- How to avoid stack overflow when using scalaz's free monad?
- Akka unit testing strategies without mocks
- Evaluating a task using a different scalaVersion than the current
- How to extract keys from nested JSON
- Performing Actions Before/After substream in akka-streams 2.4+
- Scala - Get function result in array inside foreach
- Spark Structured Streaming for appending to text file using foreach
- ClassTag causes Spark to serialize object
- How to remove new line characters in spark scala
- Simplest way to extract Option from Scala collections
- Lift's splitting single tags into pairs leads to issues
- Does SBT use the Fast Scala Compiler (fsc)?
- Scala filtering tuple list
- when create the datatable from datamodel the NullPointerException is present
- Split 1 row into multiple rows by range on 2 column values in scala
- Scala: Add values to sortedSet or array using for loop
- Spark Scala NoClassDefFoundError: org/apache/spark/Logging
- Enabling Parallelization in Spark with Partition Pushdown in MemSQL
- Modifying case class constructor parameter before setting value
- Scala Dataframe window lag function on condition