Skip to content

Commit

Permalink
Add equality minus epsilon comparison for Doubles
Browse files Browse the repository at this point in the history
  • Loading branch information
ettore committed Nov 29, 2024
1 parent 24dedd4 commit 2677756
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
22 changes: 22 additions & 0 deletions Sources/Number/Double+NYPLAdditions.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
5 changes: 1 addition & 4 deletions Sources/Number/Float+NYPLAdditions.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// StdLib+NYPLAdditions.swift
// Simplified
//
// Created by Ettore Pasquini on 6/17/20.
// Copyright © 2020 NYPL. All rights reserved.
// Copyright © 2020 The New York Public Library. All Rights Reserved.
//

import Foundation
Expand Down

0 comments on commit 2677756

Please sign in to comment.