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

Erica Sadun erica at ericasadun.com
Fri Feb 26 18:12:01 CST 2016

I have a problem with the way floating point ranges work with striding:
1.0.stride(through: 2.0, by: 0.1) returns the sequence [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9].  
Documentation: "It returns the sequence where last is less than or equal to `end`." 
(And yes, the same issue exists with tradition C-style for loops).

Would it be really horrible if the implementation and definition was changed to:  
"It returns the sequence where last is greater than or equal to `end`?"
This would offer no change for integers, and include 2.0 for floating point sequences. 

Alternatively, could there be decimalStride? Using Double but a rounding system with a fixed number of decimal places (e.g. 1, 2, 3), to ensure at least the end point is hit? It might look like:
1.0.stride(through: 2.0, by: 0.1, places: 1)

I know there have been several discussions on-list about decimal number systems (Re: Is there a need for a Decimal type? <http://article.gmane.org/gmane.comp.lang.swift.evolution/7130/match=decimal>) as well, but this could fix an ongoing annoyance without a major change.

Thanks for your thoughts,

-- Erica

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

More information about the swift-evolution mailing list