i am trying to financial app, where sum of credit and debit is 0
Why this is like this
scala> 7595.00-8544.38+949.38
res7: Double = 7.958078640513122E-13
but when sorted and added
scala> 7595.00+949.38-8544.38
res8: Double = 0.0
well the problem is numbers are at random but they always sum upto to zero
i did same with node and found same results. what is the problem.
i wrote this function it works
def sum(xs: List[Double]): Double = {
xs match {
case x :: tail => BigDecimal(x + sum(tail)).setScale(2,BigDecimal.RoundingMode.HALF_UP).toDouble
case Nil => 0
}
}
it's strange though..