Skip to content

Commit

Permalink
Reduce function compile complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
vospennikov committed Dec 28, 2023
1 parent c457241 commit 53f830e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions Sources/ClusterMap/Internal/Extensions/FloatingPoint+Equal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,27 @@ import Foundation

extension FloatingPoint {
func isNearlyEqual(to value: Self) -> Bool {
if self == value {
return true
}

let absA = abs(self)
let absB = abs(value)
let diff = abs(self - value)

if self == value {
return true
} else if self == .zero || value == .zero || (absA + absB) < .leastNormalMagnitude {
return diff < .ulpOfOne * .leastNormalMagnitude

if self == .zero || value == .zero || (absA + absB) < .leastNormalMagnitude {
return isDiffLessThanUlp(diff)
} else {
return diff / min(absA + absB, .greatestFiniteMagnitude) < .ulpOfOne
return isDiffSmallEnough(diff, absA, absB)
}
}

private func isDiffLessThanUlp(_ diff: Self) -> Bool {
return diff < .ulpOfOne * .leastNormalMagnitude
}

private func isDiffSmallEnough(_ diff: Self, _ absA: Self, _ absB: Self) -> Bool {
let sumMin = min(absA + absB, .greatestFiniteMagnitude)
return diff / sumMin < .ulpOfOne
}
}

0 comments on commit 53f830e

Please sign in to comment.