Skip to content

Commit

Permalink
Check for negative exp add
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd committed Dec 3, 2023
1 parent 2f5229b commit 6c33821
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
6 changes: 3 additions & 3 deletions test/prog_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
),
},

Expand Down
2 changes: 1 addition & 1 deletion test/test_num.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions test/test_turing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,10 +1038,10 @@ def test_algebra(self):
print(' },\n')

assert_num_counts({
"adds": 33682,
"adds": 33702,
"divs": 8390,
"exps": 9657,
"muls": 7457,
"muls": 7462,
})


Expand Down
8 changes: 8 additions & 0 deletions tm/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 6c33821

Please sign in to comment.