Repository: mpmath/mpmath (python)

Issue:
Consider adding support for special numbers in almosteq()
I.e. ``nan`` is not considered close to any other value, including ``nan`. ``inf`` and ``-inf`` are only considered close to themselves.  See also [PEP 485](https://peps.python.org/pep-0485/).

If that will be accepted, we also could use ``math.isclose()`` to implement ``almosteq()`` for the fp backend.  Probably this should be slightly faster.

Right now we have:
```pycon
>>> from mpmath import inf, mp
>>> mp.almosteq(inf, inf)
False
```
c.f.
```pycon
>>> from math import inf, isclose
>>> isclose(inf, inf)
True
```

Are there good reasons why ``almosteq()`` behave differently from the ``math.isclose()``?

Pull request description:
Support inf/nan's in ctx.almosteq()
Closes #799

Patch:
diff --git a/mpmath/ctx_base.py b/mpmath/ctx_base.py
index c10721d..3309bfa 100644
--- a/mpmath/ctx_base.py
+++ b/mpmath/ctx_base.py
@@ -191,7 +191,10 @@ def almosteq(ctx, s, t, rel_eps=None, abs_eps=None):
             False
 
         """
+        s = ctx.convert(s)
         t = ctx.convert(t)
+        if any(ctx.isinf(_) or ctx.isnan(_) for _ in [s, t]):
+            return s == t
         if abs_eps is None and rel_eps is None:
             rel_eps = abs_eps = ctx.ldexp(1, -ctx.prec+4)
         if abs_eps is None:


Test patch:
diff --git a/mpmath/tests/test_basic_ops.py b/mpmath/tests/test_basic_ops.py
index 121de90..c05da57 100644
--- a/mpmath/tests/test_basic_ops.py
+++ b/mpmath/tests/test_basic_ops.py
@@ -241,6 +241,12 @@ def test_almost_equal():
     assert mpf(1.2).ae(mpf(1.20000001), 1e-7)
     assert not mpf(1.2).ae(mpf(1.20000001), 1e-9)
     assert not mpf(-0.7818314824680298).ae(mpf(-0.774695868667929))
+    assert inf.ae(inf)
+    assert not inf.ae(-inf)
+    assert not mpf(1.2).ae(nan)
+    assert not mpf(1.2).ae(inf)
+    assert not nan.ae(nan)
+    assert not nan.ae(inf)
 
 def test_arithmetic_functions():
     ops = [(operator.add, fadd), (operator.sub, fsub), (operator.mul, fmul),