Github user srowen commented on the pull request:
https://github.com/apache/spark/pull/1787#issuecomment-51598806
Yeah, the problem is now this, essentially:
```
scala> (1.0 to (2.0, 1.0/10.0)).toArray
res1: Array[Double] = Array(1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6,
1.7000000000000002, 1.8, 1.9, 2.0)
```
The problem is that naively adding the increment makes for increasing
rounding errors as the range goes on. At 100 slices:
```
scala> (1.0 to (2.0, 1.0/100.0)).takeRight(10)
res4: scala.collection.immutable.IndexedSeq[Double] =
Vector(1.9100000000000008, 1.9200000000000008, 1.9300000000000008,
1.9400000000000008, 1.9500000000000008, 1.9600000000000009, 1.9700000000000009,
1.9800000000000009, 1.9900000000000009, 2.000000000000001)
```
Here's an attempt to write a version that is both more accurate and forces
the end of the range to be correct:
```
def range(min: Double, max: Double, steps: Int): IndexedSeq[Double] = {
val span = max - min
Range.Int(0, steps, 1).map(s => min + (s * span) / steps) :+ max
}
```
A little bit inelegant, and still not perfect, but better and fixes the
original problem still:
```
scala> range(1.0, 2.0, 10)
res5: IndexedSeq[Double] = Vector(1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7,
1.8, 1.9, 2.0)
scala> range(1.0, 2.0, 100).takeRight(10)
res7: IndexedSeq[Double] = Vector(1.9100000000000001, 1.92,
1.9300000000000002, 1.94, 1.95, 1.96, 1.97, 1.98, 1.99, 2.0)
```
What do you guys think? @nrchandan
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]