<div dir="ltr">I'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 'epsilon' and 'ULPs' 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>) -> <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 'almost equal' comparison?</div><div><br></div><div>Or, asking the same question another way, what doesn't the Swift method</div><div><span class="gmail-syntax-keyword" style="box-sizing:inherit;color:rgb(170,13,145);font-family:'SF Mono',Menlo,monospace,'SF Pro Icons';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:'SF Mono',Menlo,monospace,'SF Pro Icons';letter-spacing:-0.4050000011920929px">func</span><span style="color:rgb(51,51,51);font-family:'SF Mono',Menlo,monospace,'SF Pro Icons';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:'SF Mono',Menlo,monospace,'SF Pro Icons';letter-spacing:-0.4050000011920929px">isEqual</span><span style="color:rgb(51,51,51);font-family:'SF Mono',Menlo,monospace,'SF Pro Icons';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:'SF Mono',Menlo,monospace,'SF Pro Icons';letter-spacing:-0.4050000011920929px">to</span><span style="color:rgb(51,51,51);font-family:'SF Mono',Menlo,monospace,'SF Pro Icons';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:'SF Mono',Menlo,monospace,'SF Pro Icons';letter-spacing:-0.4050000011920929px">other</span><span style="color:rgb(51,51,51);font-family:'SF Mono',Menlo,monospace,'SF Pro Icons';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:'SF Mono',Menlo,monospace,'SF Pro Icons';letter-spacing:-0.4050000011920929px">Self</span><span style="color:rgb(51,51,51);font-family:'SF Mono',Menlo,monospace,'SF Pro Icons';letter-spacing:-0.4050000011920929px;background-color:rgb(249,250,250)">) -> </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:'SF Mono',Menlo,monospace,'SF Pro Icons';letter-spacing:-0.4050000011920929px">Bool</a></div><div><br></div><div>actually do? Does it test for equality of the binary representation of 'self' and 'other' (I assume it must given no 'precision' 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>