<div dir="ltr">You can make it even simpler with a range pattern<br><br><div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-family:monospace,monospace"><span style="color:rgb(186,45,162)">func</span> almost_equal&lt;T:<span style="color:rgb(112,61,170)">FloatingPoint</span>&gt;(<span style="color:rgb(186,45,162)">_</span> a:<span style="color:rgb(79,129,135)">T</span>, <span style="color:rgb(186,45,162)">_</span> b:<span style="color:rgb(79,129,135)">T</span>) -&gt; <span style="color:rgb(112,61,170)">Bool</span> <br>{</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-family:monospace,monospace">    <span style="color:rgb(186,45,162)">return</span> b.<span style="color:rgb(112,61,170)">nextDown</span> ... b.<span style="color:rgb(112,61,170)">nextUp<span style="color:rgb(0,0,0)"> ~= a</span><br></span></span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-family:monospace,monospace">}</span></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jun 29, 2017 at 3:05 PM, Stephen Canon via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space">I should also point out:<div><br></div><div>(a) your code can be somewhat simpler in Swift. I would probably write something along the lines of:</div><div><br><div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#ba2da2">func</span> almostEqual&lt;T: <span style="color:#703daa">FloatingPoint</span>&gt;(<span style="color:#ba2da2">_</span> a: <span style="color:#4f8187">T</span>, <span style="color:#ba2da2">_</span> b: <span style="color:#4f8187">T</span>) -&gt; <span style="color:#703daa">Bool</span> {</div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    <span style="color:#ba2da2">return</span> a &gt;= b.<span style="color:#703daa">nextDown</span> &amp;&amp; a &lt;= b.<span style="color:#703daa">nextUp</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div></div></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255)">(b) one ULP is almost never a tolerance you want to use. It’s much too small for almost all computations, and too large for most of the remaining ones.</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255)"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255)">– Steve</div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>