From c159c47ddd24927271b4c6d7e7e7401883bf7f6f Mon Sep 17 00:00:00 2001 From: Nick Drozd Date: Sun, 3 Dec 2023 12:00:45 -0600 Subject: [PATCH] Check for negative exp add --- test/prog_data.py | 6 +++--- test/test_num.py | 2 +- tm/num.py | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/test/prog_data.py b/test/prog_data.py index 2eefa8c0..da904db5 100644 --- a/test/prog_data.py +++ b/test/prog_data.py @@ -1692,9 +1692,9 @@ "(10 ** 207779)", ), "1RB 0LE 1RC 1RD 0LB ... 1LE 0RF 1RC 1LA 1LE 1RF": ( - 9791, - "(463 + (-(223 * (2 ** 7)) + ((7 * (2 ** 12)) + (-(223 * (2 ** 14)) + ((7 * (2 ** 19)) + (-(223 * (2 ** 22)) + ((57 * (2 ** 24)) + (-(223 * (2 ** 25)) + ((7 * (2 ** 30)) + (-(223 * (2 ** 28)) + ((7 * (2 ** 33)) + (-(223 * (2 ** 32)) + ((7 * (2 ** 37)) + (-(223 * (2 ** 37)) + ((7 * (2 ** 42)) + (-(223 * (2 ** 43)) + ((7 * (2 ** 48)) + ((239 * (2 ** 46)) + -(223 * (2 ** 46))))))))))))))))))))", - "(10 ** 16)", + 9808, + "(462 + (-(223 * (2 ** 6)) + ((7 * (2 ** 11)) + (-(223 * (2 ** 12)) + ((7 * (2 ** 17)) + (-(223 * (2 ** 19)) + ((7 * (2 ** 24)) + (-(223 * (2 ** 27)) + ((57 * (2 ** 29)) + (-(223 * (2 ** 30)) + ((7 * (2 ** 35)) + (-(223 * (2 ** 33)) + ((7 * (2 ** 38)) + (-(223 * (2 ** 37)) + ((7 * (2 ** 42)) + (-(223 * (2 ** 42)) + ((7 * (2 ** 47)) + ((11 * (2 ** 53)) + -(223 * (2 ** 48))))))))))))))))))))", + "(10 ** 17)", ), }, diff --git a/test/test_num.py b/test/test_num.py index 1bb5c7b4..5035fac6 100644 --- a/test/test_num.py +++ b/test/test_num.py @@ -284,7 +284,7 @@ def test_add(self): self.assert_num( (7 * Exp(3, 3)) + -(5 * Exp(3, 3)), 54, - "((7 * (3 ** 3)) + -(5 * (3 ** 3)))") + "(2 * (3 ** 3))") def test_div(self): self.assert_num( diff --git a/tm/num.py b/tm/num.py index 27719825..46d0d3b8 100644 --- a/tm/num.py +++ b/tm/num.py @@ -479,6 +479,14 @@ def __add__(self, other: Count) -> Count: except NotImplementedError: # no-cover pass + if lo == -1 and isinstance(ro, Mul): + rol, ror = ro.l, ro.r + + if (isinstance(ror, Exp) + and ror.base == r.base + and ror.exp == r.exp): + return (l + -rol) * r + elif isinstance(other, Add): lo, ro = other.l, other.r