<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jun 29, 2017, at 2:40 PM, Gavin Eadie via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">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/" class="">this</a>.<div class=""><br class=""></div><div class="">For example, for a off-by-one ULP comparison:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 252, 234);" class=""><span style="color:rgb(54,86,138)" class="">public</span> <span style="color:rgb(54,86,138)" class="">func</span> almostEqual(<span style="color:rgb(54,86,138)" class="">_</span> a: <span style="color:rgb(195,89,0)" class="">Double</span>, <span style="color:rgb(54,86,138)" class="">_</span> b: <span style="color:rgb(195,89,0)" class="">Double</span>) -> <span style="color:rgb(195,89,0)" class="">Bool</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 252, 234);" class=""> <span style="color:rgb(54,86,138)" class="">return</span> a == b ||</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(88, 126, 168); background-color: rgb(255, 252, 234);" class=""><span style="" class=""> a == </span>nextafter<span style="" class="">(b, +.</span>greatestFiniteMagnitude<span style="" class="">) ||</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(88, 126, 168); background-color: rgb(255, 252, 234);" class=""><span style="" class=""> a == </span>nextafter<span style="" class="">(b, -.</span>greatestFiniteMagnitude<span style="" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 252, 234);" class="">}</div></div><div class=""><br class=""></div><div class="">My question is whether Swift has a built in method that provides an 'almost equal' comparison?</div></div></div></blockquote><div><br class=""></div><div>It does not. It is “trivial” to implement such a comparison (just like you do here); the difficulty is entirely in guiding users in choosing a means of comparison and tolerance appropriate to their problem.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="">Or, asking the same question another way, what doesn't the Swift method</div><div class=""><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 class=""></span></div><div class=""><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)" class=""> </span><span class="gmail-syntax-identifier" style="box-sizing: inherit; 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)" class="">(</span><span class="gmail-syntax-identifier" style="box-sizing: inherit; 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)" class=""> </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)" class="">: </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)" class="">) -> </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 class=""><br class=""></div><div class="">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></div></blockquote><br class=""></div><div>isEqual implements IEEE equality, which is *not the same* as bitwise equality of representation. In particular:</div><div><br class=""></div><div>1. x != x is true when x is NaN.</div><div>2. –0 == +0, but the two values have different encodings.</div><div>3. Float80 admits non-canonical “pseudo denormal” values that compare equal to a canonical value with a different encoding.</div><div>4. IEEE 754 Decimal types (not implemented in the standard library) have *many* encodings that compare equal.</div><div><br class=""></div><div>– Steve</div></body></html>