mp.c, catacomb/__init__.py, pyke/: Fix mixed-mode arithmetic involving `float'.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 22 Oct 2019 17:31:57 +0000 (18:31 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 11 Apr 2020 11:44:21 +0000 (12:44 +0100)
commit1c0e9c88b06e3422c7afa0cf4bd3fbe166b401da
tree08034ecd1f713b5f77f2948e9c7c1389a63ed7e9
parente0a7dece879cf5f6ea6e36899214e95c27f8085b
mp.c, catacomb/__init__.py, pyke/: Fix mixed-mode arithmetic involving `float'.

This is a bit embarrassing.

>>> import catacomb as C
>>> x = C.MP(5)
>>> x == 5.1
True
>>> x < 5.1
False
>>> r = x/2
>>> r
5/2
>>> r == 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> r == 2.5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> r*1.0
5/2
>>> r*1.1
5/2
>>> r*2.0
5
>>> r*2.5
5

Fix this nonsense.

  * Change the `obvious' arithmetic operators so that they notice that
    one of the operands is a float.  Handle this by converting to a
    Python bignum and letting Python handle the arithmetic.  The result
    is a float, which seems sensible inexact contagion.

  * Introduce a rich-comparison method which also detects a float
    operand and hands off to Python.  Python seems to get this right,
    comparing the float to the bignum in its full precision, so that's a
    win.

  * Also, modify the `IntRat' code to apply inexact contagion in the
    same way.  Comparisons may be imperfect here, but that's
    surprisingly hard to get right.

The new results:

>>> import catacomb as C
>>> x = C.MP(5)
>>> x == 5.1
False
>>> x < 5.1
True
>>> r = x/2
>>> r
5/2
>>> r == 2
False
>>> r == 2.5
True
>>> r*1.0
2.5
>>> r*1.1
2.75
>>> r*2.0
5.0
>>> r*2.5
6.25
pyke.c
pyke.h