Fix negation bug in numeric power (e.g. -2^8).

parent e6c4974f
...@@ -271,7 +271,7 @@ def match_raise_numerics(node): ...@@ -271,7 +271,7 @@ def match_raise_numerics(node):
r, e = node r, e = node
if r.is_numeric() and e.is_numeric() and not e.negated: if r.is_numeric() and e.is_numeric() and not e.negated:
return [P(node, raise_numerics, (r, e))] return [P(node, raise_numerics, (r, e, node.negated))]
return [] return []
...@@ -282,9 +282,9 @@ def raise_numerics(root, args): ...@@ -282,9 +282,9 @@ def raise_numerics(root, args):
(-2) ^ 3 -> -8 (-2) ^ 3 -> -8
(-2) ^ 2 -> 4 (-2) ^ 2 -> 4
""" """
r, e = args r, e, negated = args
return Leaf(r.value ** e.value, negated=r.negated * e.value) return Leaf(r.value ** e.value, negated=r.negated * e.value + negated)
MESSAGES[raise_numerics] = _('Raise constant {1} with {2}.') MESSAGES[raise_numerics] = _('Raise constant {1} with {2}.')
...@@ -155,10 +155,16 @@ class TestRulesNumerics(RulesTestCase): ...@@ -155,10 +155,16 @@ class TestRulesNumerics(RulesTestCase):
def test_raise_numerics(self): def test_raise_numerics(self):
l1, l2 = root = tree('2 ^ 3') l1, l2 = root = tree('2 ^ 3')
self.assertEqualNodes(raise_numerics(root, (l1, l2)), L(8)) self.assertEqualNodes(raise_numerics(root, (l1, l2, 0)), L(8))
l1_neg, l2 = root = tree('(-2) ^ 2') l1_neg, l2 = root = tree('(-2) ^ 2')
self.assertEqualNodes(raise_numerics(root, (l1_neg, l2)), --L(4)) self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 0)), --L(4))
l1_neg, l2 = root = tree('(-2) ^ 3') l1_neg, l2 = root = tree('(-2) ^ 3')
self.assertEqualNodes(raise_numerics(root, (l1_neg, l2)), ---L(8)) self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 0)), ---L(8))
l1_neg, l2 = root = tree('-((-2) ^ 3)')
self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 1)), ----L(8))
l1_neg, l2 = root = tree('-(2 ^ 3)')
self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 1)), -L(8))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment