[swift-users] FloatingPoint equality ..

Taylor Swift kelvin13ma at gmail.com
Thu Jun 29 14:23:45 CDT 2017


You can make it even simpler with a range pattern

func almost_equal<T:FloatingPoint>(_ a:T, _ b:T) -> Bool
{
    return b.nextDown ... b.nextUp ~= a
}

On Thu, Jun 29, 2017 at 3:05 PM, Stephen Canon via swift-users <
swift-users at swift.org> wrote:

> I should also point out:
>
> (a) your code can be somewhat simpler in Swift. I would probably write
> something along the lines of:
>
> func almostEqual<T: FloatingPoint>(_ a: T, _ b: T) -> Bool {
>     return a >= b.nextDown && a <= b.nextUp
> }
>
> (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.
>
> – Steve
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170629/0f54e10c/attachment.html>


More information about the swift-users mailing list