[swift-evolution] [Discussion] stride behavior and a little bit of a call-back to digital numbers

Erica Sadun erica at ericasadun.com
Sat Feb 27 18:53:37 CST 2016

> As implemented, `stride` is broken for floating-point numbers. Instead of repeatedly adding the `by` interval, it should multiply the interval by successive integral values and add that to the base to avoid accruing error. Your proposal only papers over the problem.
> -Joe

Agreed. This is "papering over the problem" of accumulated floating point error.  There are two issues being addressed:

1. Semantics of `through` do not match their promise of going through the end point rather than stopping at it or before.
2. Floating point strides accumulate errors by repeatedly adding the `by` interval.

I mention issue 2 in the alternative implementations: https://gist.github.com/erica/03c398c06f6c47824429

Counting expected iterations by forming (max - min) / by, e.g. (2.0 - 1.0) / 0.1, which is 10, and performing each step as a pro-rated progression along those steps, which would remove most of the accumulated floating point errors along the way.

If you were addressing issue 2, what approach would you suggest:

* Differentiating integer strides from floating point, and minimizing floating point errors?
* Leaving the tech as-is with minimal code change with a relatively high benefit?

In both cases, I'd still prefer the semantics to *go through* the end point, not just stop at it, which is issue 1.

-- E

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160227/b6014b84/attachment.html>

More information about the swift-evolution mailing list