<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote type="cite" class="">On Mar 30, 2016, at 12:26 AM, Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">I didn't seem to ever need division. See attached playground (which<br class="">borrows shamelessly from existing code and Erica's proposal, and which<br class="">is written in Swift 2.2 because that's what I had handy).<br class=""></blockquote><br class=""><div class="">Appending the following code to the playground reveals a bug/glitch:</div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">import</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Darwin</span></div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="font-family: Menlo; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> start = </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">pow</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2.0</span><span style="font-variant-ligatures: no-common-ligatures" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">54</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="font-family: Menlo; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> end = </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">nextafter</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">start</span><span style="font-variant-ligatures: no-common-ligatures" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Double</span><span style="font-variant-ligatures: no-common-ligatures" class="">.infinity)</span></div><div style="font-family: Menlo; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> containsRepeatedValues = </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures" class="">((</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">start</span><span style="font-variant-ligatures: no-common-ligatures" class="">..&lt;</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">end</span><span style="font-variant-ligatures: no-common-ligatures" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">striding</span><span style="font-variant-ligatures: no-common-ligatures" class="">(by: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1.0</span><span style="font-variant-ligatures: no-common-ligatures" class="">))</span></div><div style="font-family: Menlo; margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">containsRepeatedValues</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span><span style="font-variant-ligatures: no-common-ligatures" class="">//prints "[18014398509481984.0, 18014398509481984.0, 18014398509481984.0]\n"</span></div><div style="font-family: Menlo; margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">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:</span></div><div style="font-family: Menlo; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> stridesForever = </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">start</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">stride</span><span style="font-variant-ligatures: no-common-ligatures" class="">(to: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">end</span><span style="font-variant-ligatures: no-common-ligatures" class="">, by: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1.0</span><span style="font-variant-ligatures: no-common-ligatures" class="">))</span></div><div style="font-family: Menlo; margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">stridesForever</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures" class="">//prints, well, nothing because it strides forever and the playground never finishes running</span></div><div style="font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div></div><div class="">The problem in both cases is not enough floating point resolution.</div><div class=""><br class=""></div><div class="">I see three potential solutions:</div><div class="">1) If a value repeats, end the sequence early.</div><div class="">2) If a value repeats, skip it.</div><div class="">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.</div><div class=""><br class=""></div><div class="">Thoughts? It seems to me that 1 &amp; 2 both have unexpected behavior of their own, but I’m not sure it matters in practice.</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div><div class=""><br class=""></div></body></html>