-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add equality minus epsilon comparison for Doubles
- Loading branch information
Showing
2 changed files
with
23 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters