[swift-evolution] Feature proposal: Range operator with step

davesweeris at mac.com davesweeris at mac.com
Thu Apr 7 22:41:33 CDT 2016

> On Mar 30, 2016, at 12:26 AM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
> I didn't seem to ever need division. See attached playground (which
> borrows shamelessly from existing code and Erica's proposal, and which
> is written in Swift 2.2 because that's what I had handy).

Appending the following code to the playground reveals a bug/glitch:
import Darwin
let start = pow(2.0, 54)
let end = nextafter(start, Double.infinity)
let containsRepeatedValues = Array((start..<end).striding(by: 1.0))
print(containsRepeatedValues) //prints "[18014398509481984.0, 18014398509481984.0, 18014398509481984.0]\n"

IMHO, this should be a bug, since the `FloatingPointStrideTo` init function has "stride != 0" as a precondition, and this behavior essentially breaks that. Although, having repeated values is probably preferable to getting in an infinite loop like this code does:
let stridesForever = Array(start.stride(to: end, by: 1.0))
print(stridesForever) //prints, well, nothing because it strides forever and the playground never finishes running

The problem in both cases is not enough floating point resolution.

I see three potential solutions:
1) If a value repeats, end the sequence early.
2) If a value repeats, skip it.
3) Accept the practical reality that sometimes floating point types just can’t have nice things, and move them to a different protocol which makes the dangers more obvious.

Thoughts? It seems to me that 1 & 2 both have unexpected behavior of their own, but I’m not sure it matters in practice.

- Dave Sweeris

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

More information about the swift-evolution mailing list