Explorar o código

Fixed an exception that occured when using a non-numeric logarithmic base

Taddeus Kroes %!s(int64=13) %!d(string=hai) anos
pai
achega
4e488c4634
Modificáronse 2 ficheiros con 14 adicións e 4 borrados
  1. 5 3
      src/rules/logarithmic.py
  2. 9 1
      tests/test_rules_logarithmic.py

+ 5 - 3
src/rules/logarithmic.py

@@ -253,10 +253,12 @@ def match_factor_out_exponent(node):
             p.append(P(node, factor_out_exponent))
     elif exp.is_numeric() and not exp.negated:
         b, a = exp.value, base.value
-        y = int(round(math.log(b, a)))
 
-        if b == a ** y:
-            p.append(P(node, make_raised_base, (y,)))
+        if not isinstance(a, str) and not isinstance(b, str):
+            y = int(round(math.log(b, a)))
+
+            if b == a ** y:
+                p.append(P(node, make_raised_base, (y,)))
 
     return p
 

+ 9 - 1
tests/test_rules_logarithmic.py

@@ -11,7 +11,8 @@
 # details.
 #
 # You should have received a copy of the GNU Affero General Public License
-# along with TRS.  If not, see <http://www.gnu.org/licenses/>.
+import math
+
 from src.rules.logarithmic import log, match_constant_logarithm, \
         base_equals_raised, logarithm_of_one, divide_same_base, \
         match_add_logarithms, add_logarithms, expand_negations, \
@@ -176,6 +177,13 @@ class TestRulesLogarithmic(RulesTestCase):
         root = tree('log(99)')
         self.assertEqualPos(match_factor_out_exponent(root), [])
 
+    def test_match_factor_out_exponent_make_raised_base_string(self):
+        root = tree('log_a(2)')
+        self.assertEqualPos(match_factor_out_exponent(root), [])
+
+        root = tree('log_e(%s)' % (math.e ** 2))
+        self.assertEqualPos(match_factor_out_exponent(root), [])
+
     def test_split_negative_exponent(self):
         root, expect = tree('log(a ^ -b), log((a ^ b) ^ -1)')
         self.assertEqual(split_negative_exponent(root, ()), expect)