test_Subtraction_large_from_small

This commit is contained in:
Simon Forman 2022-10-04 16:41:09 -07:00
parent 4cea474166
commit 217adaa318
1 changed files with 14 additions and 3 deletions

View File

@ -19,6 +19,9 @@ class BigInt:
initial = -initial initial = -initial
self.digits = list(self.digitize(initial)) # List of OberonInt. self.digits = list(self.digitize(initial)) # List of OberonInt.
def __repr__(self):
return f'BigInt({self.to_int()})'
@staticmethod @staticmethod
def digitize(n): def digitize(n):
if n < 0: if n < 0:
@ -125,7 +128,7 @@ class BigInt:
if not a.abs_gt_abs(b): if not a.abs_gt_abs(b):
#print(f'abs({a.to_int()}) < abs({b.to_int()})') #print(f'abs({a.to_int()}) < abs({b.to_int()})')
x = b._subtract_smaller(a) x = b._subtract_smaller(a)
x.sign = not x.sign #x.sign = not x.sign
return x return x
#print(f'abs({a.to_int()}) > abs({b.to_int()})') #print(f'abs({a.to_int()}) > abs({b.to_int()})')
return a._subtract_smaller(b) return a._subtract_smaller(b)
@ -139,7 +142,6 @@ class BigInt:
other.digits, other.digits,
fillvalue=zero, fillvalue=zero,
) )
#P(list(Z))
for a, b in Z: for a, b in Z:
carry, digit = a.sub_with_carry(b, carry) carry, digit = a.sub_with_carry(b, carry)
out.append(digit) out.append(digit)
@ -372,7 +374,7 @@ class BigIntTest(unittest.TestCase):
print() print()
print(x + y) print(x + y)
def test_Subtraction(self): def test_Subtraction_small_from_large(self):
n = 12345678901234567898090123445678990 n = 12345678901234567898090123445678990
m = 901234567898090 m = 901234567898090
x = BigInt(n) x = BigInt(n)
@ -381,6 +383,15 @@ class BigIntTest(unittest.TestCase):
t = z.to_int() t = z.to_int()
self.assertEqual(t, n - m) self.assertEqual(t, n - m)
def test_Subtraction_large_from_small(self):
n = 901234567898090
m = 12345678901234567898090123445678990
x = BigInt(n)
y = BigInt(m)
z = x - y
t = z.to_int()
self.assertEqual(t, n - m)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()