Minor cleanup.

This commit is contained in:
Simon Forman 2022-10-04 22:18:22 -07:00
parent beafe3aff0
commit 918e77b678
1 changed files with 8 additions and 13 deletions

View File

@ -99,34 +99,29 @@ class BigInt:
acc = BigInt() acc = BigInt()
for i, digit in enumerate(other.digits): for i, digit in enumerate(other.digits):
intermediate_result = self._mul_one_digit(i, digit) acc = acc + self._mul_one_digit(i, digit)
#print(intermediate_result)
acc = acc + intermediate_result
acc.sign = not (self.sign ^ other.sign) acc.sign = not (self.sign ^ other.sign)
return acc return acc
def _mul_one_digit(self, power, n): def _mul_one_digit(self, power, n):
# Some of this should go in a method of OberonInt? # Some of this should go in a method of OberonInt?
out = [zero] * power digits = [zero] * power
carry = zero carry = zero
for digit in self.digits: for digit in self.digits:
# In the Oberon RISC the high half of multiplication high, low = digit * n
# is put into the special H register. c, p = low + carry
H, product = digit * n digits.append(p)
c, p = product + carry carry = high
out.append(p)
carry = H
if c: if c:
z, carry = carry + one z, carry = carry + one
assert not z, repr(z) assert not z, repr(z)
if carry.value: if carry.value:
assert carry.value > 0 assert carry.value > 0
out.append(carry) digits.append(carry)
result = BigInt() result = BigInt()
result.digits = out result.digits = digits
return result return result
def add_like_signs(self, other): def add_like_signs(self, other):
''' '''
Add a BigInt of the same sign as self. Add a BigInt of the same sign as self.