<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Another alternative would be to sub-divide the generator into segments of Int.max elements. This means that error may still potentially creep in if there are enough of these steps, but it would be considerably less error-prone than the accumulation method, without the same performance penalty of BigInt. Basically if you have a start and end index you move the start index up every Int.max (or Uint.max) elements so long as start index is less than end index.</div><div class=""><br class=""></div><div class="">This should allow for a lot less error than accumulation, without the performance/memory overhead that a big integer could introduce, though it would still have some small inaccuracy (but then so does everything involving floating point types).</div><br class=""><div><blockquote type="cite" class=""><div class="">On 2 Apr 2016, at 18:43, Thorsten Seitz via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">Am 01.04.2016 um 00:23 schrieb Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt;:</div><br class="Apple-interchange-newline"><div class="">All true. I call that Erica's solution because her proposal is where I first found it sketched out.<br class=""><br class="">I'm not convinced that Erica's solution is definitely the right answer because:<br class=""><br class="">(a) Use of an iteration counter of type Int to stride through Doubles is an implementation detail which is not an obviously correct choice; users might find it surprising that how many steps they get for StrideTo&lt;Double&gt; is constrained by Int.max<br class=""></div></blockquote><div class=""><br class=""></div><div class="">Couldn’t we use a BigInt internally (if necessary)? This would make this problem disappear.</div></div><div class=""><br class=""></div><div class="">-Thorsten</div><div class=""><br class=""></div><div class=""><br class=""><blockquote type="cite" class=""><div class="">(b) I'm not completely certain that there is no use case for a loop with more than Int.max steps so long as you break before the end, so I'm not completely certain that an error right off the bat is the most ideal behavior; for example, someone may wish to increment by a user-supplied epsilon from one user-supplied value to another but break after a certain amount of time has elapsed<br class=""><br class="">(c) I agree with you that it's Swiftier to do nothing than to start returning approximately correct values, but in a scenario such as `for _ in stride(from: 0, to: DBL_MAX, by: someAbsurdlySmallValue) { }` it may not matter (I cannot imagine a use case for this ridiculous loop, but for the sake of argument here let's take it); one alternative solution someone might propose, for example, would be to fall back to the old error-accumulating algorithm after the iteration counter has reached its max possible value<br class=""><br class="">So I guess the feedback I'm interested in is:<br class=""><br class="">- Would you be surprised to find that Stride&lt;Double&gt; may become constrained by an upper limit in the number of steps?<br class=""><br class="">- If not, would it irk you that such a limit is based on the size of a totally unrelated numeric type (namely, Int) which is an implementation detail? Would you prefer that the limit be something related to the nature of the type itself (for example, a maximum number of steps for StrideTo&lt;Double&gt; that reflects the maximum exactly representable integer in a Double)?<br class=""><br class="">- If there is to be an upper limit on steps, would you prefer an error when the Stride is being initialized or when the iteration counter overflows?<br class=""><br class="">- Would you rather instead be able to stride indefinitely, as is currently the case in Swift 2, accepting that error will start accumulating at some point?<br class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Thu, Mar 31, 2016 at 4:33 PM Howard Lovatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">If you define a range as range[i] = first + i * stride where i is an Int then this&nbsp;generates an error when there are more than Int_Max steps, see code previously posted. The error is generated when the range is formed, which is ideal since an error part way along an iteration or a never ending iteration would be difficult to track&nbsp;down.&nbsp;<br class=""><br class="">On Friday, 1 April 2016, Stephen Canon via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br class="">
&gt; On Mar 31, 2016, at 11:16 AM, Rainer Brockerhoff via swift-evolution &lt;<a class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; On 3/31/16 15:06, Dave Abrahams via swift-evolution wrote:<br class="">
&gt;&gt;<br class="">
&gt;&gt; on Thu Mar 31 2016, Xiaodi Wu &lt;<a href="http://xiaodi.wu-at-gmail.com/" target="_blank" class="">xiaodi.wu-AT-gmail.com</a>&gt; wrote:<br class="">
&gt;&gt;<br class="">
&gt;&gt;&gt; Thoughts on an edge case: so long as it's possible to use<br class="">
&gt;&gt;&gt; `stride(from:to:by:)` with Double, we'll need to figure out what<br class="">
&gt;&gt;&gt; happens when you have `stride(from: 0.0, to: DBL_MAX, by: DBL_MIN)`.<br class="">
&gt;&gt;&gt; Bounds may be unknown at compile time, obviously.<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; Currently (this is by reasoning through the code, not actually<br class="">
&gt;&gt;&gt; observing it run), `for i in stride(from: 0.0, to: DBL_MAX, by:<br class="">
&gt;&gt;&gt; DBL_MIN) { }` degenerates into an infinite loop once you reach<br class="">
&gt;&gt;&gt; sufficient large values such that `current + stride == current`, which<br class="">
&gt;&gt;&gt; for a stride of DBL_MIN should happen pretty quickly.<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; In Erica's proposed floating point Stride, an Int is used to count<br class="">
&gt;&gt;&gt; iterations (and iterations need to be counted in order to avoid<br class="">
&gt;&gt;&gt; accumulating error). Thus, one must break from `for i in stride(from:<br class="">
&gt;&gt;&gt; 0.0, to: DBL_MAX, by: DBL_MIN) { }` before the iteration counter<br class="">
&gt;&gt;&gt; overflows or it will trap. IMO, trapping at some point is fine, but I<br class="">
&gt;&gt;&gt; think a limit of Int.max iterations might be rather arbitrary for a<br class="">
&gt;&gt;&gt; StrideTo&lt;Double&gt; (or whatever it will be named) and I'm not sure how<br class="">
&gt;&gt;&gt; one can justify why the behavior of StrideTo&lt;Double&gt; would change from<br class="">
&gt;&gt;&gt; machine to machine based on the size of Int.<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; I've been waffling between using an Int counter as Erica does or a<br class="">
&gt;&gt;&gt; counter of type Strideable.Stride in `StrideTo&lt;Strideable where<br class="">
&gt;&gt;&gt; Strideable.Stride : FloatingPoint&gt;`. In the latter alternative, no<br class="">
&gt;&gt;&gt; trapping occurs, but error begins to accumulate when the iteration<br class="">
&gt;&gt;&gt; counter is too large to represent integers exactly (e.g., 2^53 for<br class="">
&gt;&gt;&gt; Double). In that case, `for i in stride(from: 0.0, to: DBL_MAX, by:<br class="">
&gt;&gt;&gt; DBL_MIN) { }` degenerates into an infinite loop eventually (once<br class="">
&gt;&gt;&gt; `iterationCount + 1.0 == iterationCount`) and never traps, which I'm<br class="">
&gt;&gt;&gt; not sure I like, but a limit of 2^53 iterations bears at least a<br class="">
&gt;&gt;&gt; rational connection to Double and is known at compile time independent<br class="">
&gt;&gt;&gt; of the supplied bounds. We could alternatively return nil on reaching<br class="">
&gt;&gt;&gt; 2^53 iterations, trap, etc.<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; Comments?<br class="">
&gt;&gt;<br class="">
&gt;&gt; I think I want to hear Steve Canon's input on this one.&nbsp; I defer to him<br class="">
&gt;&gt; on most things numeric.<br class="">
&gt;<br class="">
&gt; In particular, should Steve confirm that the IEEE754 Decimal128 format<br class="">
&gt; is being worked on, and if simple decimal constants like those in<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;`for i in stride(from: 0.0, to: DBL_MAX, by: DBL_MIN) { }`<br class="">
&gt; will be type-inferred as Decimal128, all that would "just work".<br class="">
<br class="">
Decimal is something that I would like to see happen.&nbsp; However, I would not expect any such proposal to result in that loop being type inferred to Decimal, since the to: and by: values are explicitly (binary floating-point) Doubles.<br class="">
<br class="">
I also don’t think that such a loop is particularly useful.&nbsp; For floating-point types, something like stride(from: T, to: T, steps: Int) seems safer and more workable to me (this is just my immediate reaction, I haven’t thought this through in detail, and am likely to change my mind if someone makes a good case).<br class="">
<br class="">
– Steve<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote><br class=""><br class="">-- <br class="">-- Howard.<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>