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?
This commit is contained in:
parent
690f157ac8
commit
53bea5f59b
|
|
@ -20,21 +20,27 @@ class OberonInt:
|
||||||
'''
|
'''
|
||||||
Return carry bit and new value.
|
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
|
n = self.value.value + other.value.value
|
||||||
carry = not is_i32(n)
|
carry = not is_i32(n)
|
||||||
if carry:
|
if carry:
|
||||||
n &= (2**31-1)
|
n &= (2**31-1)
|
||||||
return int(carry), OberonInt(n)
|
return int(carry), OberonInt(n)
|
||||||
|
|
||||||
|
__radd__ = __add__
|
||||||
|
|
||||||
def negate(self):
|
def negate(self):
|
||||||
# Instead of binary ops, just cheat:
|
# Instead of binary ops, just cheat:
|
||||||
return OberonInt(-self.value.value)
|
return OberonInt(-self.value.value)
|
||||||
|
|
||||||
def __sub__(self, other):
|
def __sub__(self, other):
|
||||||
assert isinstance(other, OberonInt)
|
if not isinstance(other, OberonInt):
|
||||||
|
other = OberonInt(other)
|
||||||
return self + other.negate()
|
return self + other.negate()
|
||||||
|
|
||||||
|
__rsub__ = __sub__
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
#b = bin(self.value.value & (2**32-1))
|
#b = bin(self.value.value & (2**32-1))
|
||||||
return f'OberonInt({self.value.value})'
|
return f'OberonInt({self.value.value})'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue