From 53bea5f59b8ec6222974724342072332a1cdc187 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Tue, 4 Oct 2022 08:53:32 -0700 Subject: [PATCH] That was easy... but it breaks associativity or is it commutivity? Anyway you can't add THREE or more things in one expression (yet) because adding OberonInt objects returns a two-tuple. I could adjust the semantics of ObInt to accept such two-tuples and do add_with_carry() but that's probably more trouble than it's worth. Just gotta be careful with math expressions, eh? --- bigjoyints/big.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bigjoyints/big.py b/bigjoyints/big.py index c3bf147..fab45df 100644 --- a/bigjoyints/big.py +++ b/bigjoyints/big.py @@ -20,21 +20,27 @@ class OberonInt: ''' Return carry bit and new value. ''' - assert isinstance(other, OberonInt) + if not isinstance(other, OberonInt): + other = OberonInt(other) n = self.value.value + other.value.value carry = not is_i32(n) if carry: n &= (2**31-1) return int(carry), OberonInt(n) + __radd__ = __add__ + def negate(self): # Instead of binary ops, just cheat: return OberonInt(-self.value.value) def __sub__(self, other): - assert isinstance(other, OberonInt) + if not isinstance(other, OberonInt): + other = OberonInt(other) return self + other.negate() + __rsub__ = __sub__ + def __repr__(self): #b = bin(self.value.value & (2**32-1)) return f'OberonInt({self.value.value})'