From 46aaa613578844e151b6fb4a81d6617515742dd0 Mon Sep 17 00:00:00 2001 From: Ettore Pasquini Date: Fri, 29 Nov 2024 13:12:21 -0800 Subject: [PATCH] Allow Double to use equality minus epsilon comparison --- Sources/Number/Double+NYPLAdditions.swift | 22 +++++++++++++++++ Sources/Number/Float+NYPLAdditions.swift | 30 ----------------------- 2 files changed, 22 insertions(+), 30 deletions(-) create mode 100644 Sources/Number/Double+NYPLAdditions.swift delete mode 100644 Sources/Number/Float+NYPLAdditions.swift diff --git a/Sources/Number/Double+NYPLAdditions.swift b/Sources/Number/Double+NYPLAdditions.swift new file mode 100644 index 0000000..6ec0fce --- /dev/null +++ b/Sources/Number/Double+NYPLAdditions.swift @@ -0,0 +1,22 @@ +// +// Created by Ettore Pasquini on 11/29/24. +// Copyright © 2024 The New York Public Library. All Rights Reserved. +// + +import Foundation + +infix operator =~= : ComparisonPrecedence + +public extension Double { + + /// Performs equality check minus an epsilon + /// - Returns: `true` if the numbers differ by less than the epsilon, + /// `false` otherwise. + static func =~= (a: Double, b: Double?) -> Bool { + guard let b = b else { + return false + } + + return abs(a - b) < Double.ulpOfOne + } +} diff --git a/Sources/Number/Float+NYPLAdditions.swift b/Sources/Number/Float+NYPLAdditions.swift deleted file mode 100644 index 817e4f3..0000000 --- a/Sources/Number/Float+NYPLAdditions.swift +++ /dev/null @@ -1,30 +0,0 @@ -// -// StdLib+NYPLAdditions.swift -// Simplified -// -// Created by Ettore Pasquini on 6/17/20. -// Copyright © 2020 NYPL. All rights reserved. -// - -import Foundation - -infix operator =~= : ComparisonPrecedence - -public extension Float { - - /// Performs equality check minus an epsilon - /// - Returns: `true` if the numbers differ by less than the epsilon, - /// `false` otherwise. - static func =~= (a: Float, b: Float?) -> Bool { - guard let b = b else { - return false - } - - return abs(a - b) < Float.ulpOfOne - } - - func roundTo(decimalPlaces: Int) -> String { - return String(format: "%.\(decimalPlaces)f%%", self) as String - } -} -