Skip to content

Instantly share code, notes, and snippets.

@erica
Created February 27, 2016 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/152141917a1e7ac25290 to your computer and use it in GitHub Desktop.
Save erica/152141917a1e7ac25290 to your computer and use it in GitHub Desktop.

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?) as well, but this could fix an ongoing annoyance without a major change.

Thanks for your thoughts,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment