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@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@swift.org > https://lists.swift.org/mailman/listinfo/swift-users > >
_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users