Symbol to is deprecated. use BigDecimal range instead

Why would you want that? That seems extremely surprising. For example, if you try with (10.2, 10.8, 0.3) you get four items where you would think you need three, and the last two are almost identical (10.2, 10.5, 10.799999999999999, 10.8).

1 Like

Yes, that is my point. When there’s a gap at the end, then it is unclear how big the gap needs to be for it to be included as the final iteration. Not enough information is given in the parameters of the function. To be completely specified, the function should take a 4th argument, epsilon:Double that says skip the final iteration if the delta is less than epsilon.

If the argument of the function specifies the step, then you need to know whether to include the last gap if it is less than step.

If you know how many iterations you want, then you should use the version which takes number-of-steps as parameter, not step-size.

As I mentioned above, in order to tell whether two Doubles are almost identical, you need to specify some epsilon.

How about:


def doubleRange(x0: Double, x1: Double, step: Double): Seq[Double] = {
    require((x1 - x0)/step > 0.0)
    doubleRange(x0, x1, Math.round((x1 - x0)/step).toInt)
  }

  def doubleRange(x0: Double, x1: Double, steps: Int): Seq[Double] = {
    require(steps > 0)
    (0 to steps).map(i => x0*(steps - i)/steps + x1*i/steps)
}

If we replace Math.round with error parameter then we’ll something like this (not tested):

def doubleRange(x0: Double, x1: Double, step: Double, error: Double): Seq[Double] = {
  require((x1 - x0)/step > 0.0)
  doubleRange(x0, x1, ((x1 - x0)/step + error).floor.toInt)
}

def doubleRange(x0: Double, x1: Double, steps: Int): Seq[Double] = {
  require(steps > 0)
  (0 to steps).map(i => x0*(steps - i)/steps + x1*i/steps)
}