Skip to content

Commit

Permalink
fix for comparing infinities (#16122)
Browse files Browse the repository at this point in the history
  • Loading branch information
planetis-m authored Nov 25, 2020
1 parent 57bd645 commit e220f75
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/pure/math.nim
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ proc almostEqual*[T: SomeFloat](x, y: T; unitsInLastPlace: Natural = 4): bool {.
runnableExamples:
doAssert almostEqual(3.141592653589793, 3.1415926535897936)
doAssert almostEqual(1.6777215e7'f32, 1.6777216e7'f32)
doAssert almostEqual(Inf, Inf)
doAssert almostEqual(-Inf, -Inf)
doAssert almostEqual(Inf, -Inf) == false
doAssert almostEqual(-Inf, Inf) == false
doAssert almostEqual(Inf, NaN) == false
doAssert almostEqual(NaN, NaN) == false

if x == y:
# short circuit exact equality -- needed to catch two infinities of
# the same sign. And perhaps speeds things up a bit sometimes.
return true
let diff = abs(x - y)
result = diff <= epsilon(T) * abs(x + y) * T(unitsInLastPlace) or
diff < minimumPositiveValue(T)
Expand Down

0 comments on commit e220f75

Please sign in to comment.