summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/pure/math.nim11
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim
index af31a5785..ccd766f46 100644
--- a/lib/pure/math.nim
+++ b/lib/pure/math.nim
@@ -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)