From: Mark Wooding Date: Tue, 22 Oct 2019 17:31:57 +0000 (+0100) Subject: mp.c, catacomb/__init__.py, pyke/: Fix mixed-mode arithmetic involving `float'. X-Git-Url: https://git.distorted.org.uk/~mdw/pyke/commitdiff_plain/1c0e9c88b06e3422c7afa0cf4bd3fbe166b401da?hp=1c0e9c88b06e3422c7afa0cf4bd3fbe166b401da 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 "", line 1, in TypeError: an integer is required >>> r == 2.5 Traceback (most recent call last): File "", line 1, in 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 ---