The iteration counter will be floating point and the iterator will break down after incrementing by one is no longer exactly representable. It has been made clear on this list that there is a strong preference that no floating point stride should degenerate into an infinite loop due to this issue. Thus, there will be a limit at the time a floating point stride is created of 2^53 steps (in the case of a Double--some thought is necessary for a Float). As currently mocked up the logic is whether start + 2^53 * stride gets you to/through the end. If not, precondition failure.<br><div class="gmail_quote"><div dir="ltr">On Sun, Apr 10, 2016 at 7:41 AM &lt;<a href="mailto:davesweeris@mac.com">davesweeris@mac.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I understand (and agree with) 3/4 of that… Why do we want to prevent striding *to* an infinity? I mean, yeah it’ll take a long time to get there, but with the new floating point stride code, a floating point value will *eventually* “overflow” into infinity (or `iteration += 1` will overflow and crash), it’s just that at that point there isn’t a straight-forward way to go the other direction anymore.<div><br><div>Actually, striding from an infinity should be ok, too, as long as it’s not the actual starting point:<div><div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(187,44,162)">let</span><span style="color:rgb(0,0,0)"> x = -</span><span style="color:rgb(112,61,170)">Double</span><span style="color:rgb(0,0,0)">.infinity ... </span><span style="color:rgb(39,42,216)">0.0</span><span style="color:rgb(0,0,0)"> </span><span>// Big Problems in, um, Non-Little Loops… or something… (and apologies to Kurt Russell)</span></div></div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:#bb2ca2">let</span><span style="color:#000000"> x = -</span><span style="color:#703daa">Double</span><span style="color:#000000">.infinity &lt;.. </span><span style="color:#272ad8">0.0</span><span style="color:#000000"> </span><span>// starts at `nextafter(start, end)` (-1.797693134862316e+308, in this case)</span></div></div><div><br></div><div>If the infinities are definitely out even as exclusive endpoints, can the floating point types get min/max properties like the integer types have?</div><div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:#bb2ca2">let</span><span style="color:#000000"> x = </span><span style="color:#703daa">Double</span><span style="color:#000000">.min ... </span><span style="color:#000000"><span style="color:rgb(112,61,170)">Double</span><span>.max</span> </span><span>// same as -1.797693134862316e+308 ... </span>1.797693134862316e+308, but way easier to write</div><div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(187,44,162)">let</span><span style="color:rgb(0,0,0)"> x = </span><span style="color:rgb(0,0,0)"><span style="color:rgb(112,61,170)">Float</span><span>.min ... <span style="color:rgb(112,61,170)">Float</span><span><span>.max</span> </span><span style="color:rgb(0,132,0)">// same as -3.402823e+38<span> ... 3.402823e+38</span></span></span></span></div></div><div><span style="color:rgb(0,0,0)"><span><span style="color:rgb(0,132,0)"><span><br></span></span></span></span></div><div>- Dave Sweeris</div></div></div></div></div><div style="word-wrap:break-word"><div><div><div><div><br><div><blockquote type="cite"><div>On Apr 10, 2016, at 12:05 AM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br><div><div style="white-space:pre-wrap">We will be proposing exactly that which you&#39;ve put in parentheses, i.e. floating point types will get their own strides, and it will be a precondition failure to try to stride from or to infinity or nan :)<br></div><br><div class="gmail_quote"><div dir="ltr">On Sun, Apr 10, 2016 at 4:47 AM &lt;<a href="mailto:davesweeris@mac.com" target="_blank">davesweeris@mac.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>It’s not a matter of floating point error accumulation… At least on my machine, once a Double hits +/-∞, there’s no way that I know of to get back to normal floating point numbers. That is to say, for *all* normal, finite values of x, &quot;-Double.infinity + x&quot; will just return “-inf&quot;. If x is to equal Double.infinity, Double.NaN, or Double.quietNaN, then it’ll return “nan” (which, incidentally, will fail the regular equality test… Double.NaN isn’t even equal to itself; I think checking the floating point class is the way to do it).</div><div><br></div><div>I could easily be missing something, but AFAICT the only way to always get the correct sequence (without splitting the floating point types off into their own thing) is either have a negative stride swap start and end *before* the StrideTo starts generating values (that is, *not* by just calling `.reverse()` on something with a positive stride), or to allow “0 ..&lt; -Double.infinity” to be a valid range (with the negative stride being implied).</div><div><br></div><div>- Dave Sweeris</div></div><div style="word-wrap:break-word"><div><br></div><div><blockquote type="cite"><div>On Apr 9, 2016, at 6:59 PM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br><div><div style="white-space:pre-wrap">Yikes. Not too concerned about the infinite loop issue, as floating point strides when fixed to avoid error accumulation will necessarily enforce a finite number of steps. However, you&#39;re talking a regular, not-at-all-lazy Array being returned? That would be not good at all...<br></div><br><div class="gmail_quote"><div dir="ltr">On Sun, Apr 10, 2016 at 12:29 AM Dave via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On Apr 9, 2016, at 4:33 AM, Haravikk via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word"><div>While I’m in favour of the basic idea I think the operator selection is too complex, and I’m not sure about the need for negative strides. Really all I want are the following:</div><div><br></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>(0 ... 6).striding(by: 2)<span style="white-space:pre-wrap">        </span>// [0, 2, 4, 6]<span style="white-space:pre-wrap">                </span>x from 0 to 6</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>(0 ..&lt; 6).striding(by: 2)<span style="white-space:pre-wrap">        </span>// [0, 2, 4]<span style="white-space:pre-wrap">                </span>x from 0 while &lt;6</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>(6 ... 0).striding(by: 2)<span style="white-space:pre-wrap">        </span>// [6, 4, 2, 0]<span style="white-space:pre-wrap">                </span>x from 6 to 0</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>(6 ..&gt; 0).striding(by: 2)<span style="white-space:pre-wrap">        </span>// [6, 4, 2]<span style="white-space:pre-wrap">                </span>x from 6 while &gt;0</font></div><div><br></div><div>Everything else should be coverable either by flipping the order, or using .reverse(). The main advantage is that there’s only one new operator to clarify the 6 ..&gt; 0 case, though you could always just reuse the existing operator if you just interpret it as “x from 6 to, but not including, 0&quot;</div></div></div></blockquote></div><br></div><div style="word-wrap:break-word"><div>`.reverse()` returns an array, though, not a StrideTo&lt;&gt;, which means it’ll get in an infinite loop on infinite sequences. This works fine:</div><div><span><span><div style="font-family:Menlo;margin:0px;line-height:normal"><div style="margin:0px;line-height:normal"><span style="color:#bb2ca2">for</span><span> i </span><span style="color:#bb2ca2">in</span><span> </span><span style="color:#31595d">stride</span><span>(from: </span><span style="color:#272ad8">0.0</span><span>, to: </span><span style="color:#703daa">Double</span><span>.infinity, by: </span><span style="color:#703daa">M_PI</span><span>) {</span></div><div style="margin:0px;line-height:normal;color:rgb(49,89,93)"><span>    </span><span style="color:#bb2ca2">if</span><span> </span><span>someTestInvolving</span><span>(i) { </span><span style="color:#bb2ca2">break</span><span> }</span></div><div style="margin:0px;line-height:normal"><span>    ...</span></div><div style="margin:0px;line-height:normal"><span>}</span></div><div><span><br></span></div></div><div><span>But this never even starts executing the loop because of the infinite loop inside `.reverse()`:</span></div><div><span><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">for</span><span> i </span><span style="color:#bb2ca2">in</span><span> </span><span style="color:#31595d">stride</span><span>(from: -</span><span style="color:#703daa">Double</span><span>.infinity, to: </span><span style="color:#272ad8">0.0</span><span>, by: </span><span style="color:#703daa">M_PI</span><span>).</span><span style="color:#3d1d81">reverse</span><span>() {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span>    </span><span style="color:#bb2ca2">if</span><span> </span><span>someTestInvolving</span><span>(i) { </span><span style="color:#bb2ca2">break</span><span> }</span></div><div style="font-family:Menlo;margin:0px;line-height:normal"><span>    ...</span></div><div style="margin:0px;line-height:normal;font-family:Menlo">}</div></span></div></span></span></div><div><br></div><div>- Dave Sweeris</div></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>
</div></blockquote></div><br></div></blockquote></div>
</div></blockquote></div><br></div></div></div></div></div></blockquote></div>