<div dir="ltr">I&#39;ve spent a fascinating evening and morning in the arcane depths of floating point, specifically researching the comparison of two floating point numbers.  I pretty much understand how to do this with a combination of &#39;epsilon&#39; and &#39;ULPs&#39; after reading <a href="https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/">this</a>.<div><br></div><div>For example, for a off-by-one ULP comparison:</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,252,234)"><span style="color:rgb(54,86,138)">public</span> <span style="color:rgb(54,86,138)">func</span> almostEqual(<span style="color:rgb(54,86,138)">_</span> a: <span style="color:rgb(195,89,0)">Double</span>, <span style="color:rgb(54,86,138)">_</span> b: <span style="color:rgb(195,89,0)">Double</span>) -&gt; <span style="color:rgb(195,89,0)">Bool</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,252,234)">    <span style="color:rgb(54,86,138)">return</span> a == b ||</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(88,126,168);background-color:rgb(255,252,234)"><span style="color:rgb(0,0,0)">           a == </span>nextafter<span style="color:rgb(0,0,0)">(b, +.</span>greatestFiniteMagnitude<span style="color:rgb(0,0,0)">) ||</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(88,126,168);background-color:rgb(255,252,234)"><span style="color:rgb(0,0,0)">           a == </span>nextafter<span style="color:rgb(0,0,0)">(b, -.</span>greatestFiniteMagnitude<span style="color:rgb(0,0,0)">)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,252,234)">}</p></div><div><br></div><div>My question is whether Swift has a built in method that provides an &#39;almost equal&#39; comparison?</div><div><br></div><div>Or, asking the same question another way, what doesn&#39;t the Swift method</div><div><span class="gmail-syntax-keyword" style="box-sizing:inherit;color:rgb(170,13,145);font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;font-size:15px;letter-spacing:-0.4050000011920929px"><br></span></div><div><span class="gmail-syntax-keyword" style="box-sizing:inherit">     </span><span class="gmail-syntax-keyword" style="box-sizing:inherit;color:rgb(170,13,145);font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;letter-spacing:-0.4050000011920929px">func</span><span style="color:rgb(51,51,51);font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;letter-spacing:-0.4050000011920929px;background-color:rgb(249,250,250)"> </span><span class="gmail-syntax-identifier" style="box-sizing:inherit;color:rgb(0,0,0);font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;letter-spacing:-0.4050000011920929px">isEqual</span><span style="color:rgb(51,51,51);font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;letter-spacing:-0.4050000011920929px;background-color:rgb(249,250,250)">(</span><span class="gmail-syntax-identifier" style="box-sizing:inherit;color:rgb(0,0,0);font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;letter-spacing:-0.4050000011920929px">to</span><span style="color:rgb(51,51,51);font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;letter-spacing:-0.4050000011920929px;background-color:rgb(249,250,250)"> </span><span class="gmail-syntax-param-name" style="box-sizing:inherit;color:rgb(64,64,64);font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;letter-spacing:-0.4050000011920929px">other</span><span style="color:rgb(51,51,51);font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;letter-spacing:-0.4050000011920929px;background-color:rgb(249,250,250)">: </span><span class="gmail-symbolref" style="box-sizing:inherit;color:rgb(92,38,153);font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;letter-spacing:-0.4050000011920929px">Self</span><span style="color:rgb(51,51,51);font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;letter-spacing:-0.4050000011920929px;background-color:rgb(249,250,250)">) -&gt; </span><a class="gmail-symbolref" href="https://developer.apple.com/documentation/swift/bool" style="box-sizing:inherit;color:rgb(92,38,153);text-decoration:none;font-family:&#39;SF Mono&#39;,Menlo,monospace,&#39;SF Pro Icons&#39;;letter-spacing:-0.4050000011920929px">Bool</a></div><div><br></div><div>actually do?  Does it test for equality of the binary representation of &#39;self&#39; and &#39;other&#39; (I assume it must given no &#39;precision&#39; argument) .. I read it follows the IEEE meaning of equality but that document is not on my bookshelf and is quite expensive!</div><div><br></div><div>Apologies if this has been asked and answered before .. Gavin</div></div>