I'm trying to build a collection of Doubles that has a range and a step. When I use the Array.iterate
method I get strange floating point errors like such:
scala> Array.iterate[Double](0.0, 10)(0.1+)
res0: Array[Double] = Array(0.0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7, 0.7999999999999999, 0.8999999999999999)
It seems odd that a small range with a small step would cause such imprecision. I'm aware there are other ways I could do this (e.g. Array.iterate[Int](0, 10)(1+).map(i => i.toDouble / 10.0)
) but I'm baffled that a built in collection method would perform so badly. Is there a reason for this or am I being a dolt and doing it the wrong way?