Repository: sympy/sympy

Issue:
RuntimeError in limit
`Limit(tan(x)**(2**(sqrt(pi))), x, oo, dir='-')` gives `RuntimeError`

Discussion:

Patch:
diff --git a/sympy/calculus/util.py b/sympy/calculus/util.py
index c81b77fe5e..7174d92c28 100644
--- a/sympy/calculus/util.py
+++ b/sympy/calculus/util.py
@@ -1338,9 +1338,10 @@ def __pow__(self, other):
                                 return AccumBounds(0, real_root(self.max, den))
                     return AccumBounds(real_root(self.min, den),
                                        real_root(self.max, den))
-                num_pow = self**num
-                return num_pow**(1 / den)
-            return Pow(self, other, evaluate=False)
+                if den!=1:
+                    num_pow = self**num
+                    return num_pow**(1 / den)
+            return AccumBounds(-oo, oo)
 
         return NotImplemented
 
diff --git a/sympy/polys/polytools.py b/sympy/polys/polytools.py
index 1f15d8d98c..e148ad1a1a 100644
--- a/sympy/polys/polytools.py
+++ b/sympy/polys/polytools.py
@@ -6625,7 +6625,7 @@ def cancel(f, *gens, **args):
         (F, G), opt = parallel_poly_from_expr((p, q), *gens, **args)
     except PolificationFailed:
         if not isinstance(f, (tuple, Tuple)):
-            return f.expand()
+            return f
         else:
             return S.One, p, q
     except PolynomialError as msg:


Test patch:
diff --git a/sympy/calculus/tests/test_util.py b/sympy/calculus/tests/test_util.py
index a3d34a6169..5035cceb4e 100644
--- a/sympy/calculus/tests/test_util.py
+++ b/sympy/calculus/tests/test_util.py
@@ -452,14 +452,14 @@ def test_AccumBounds_pow():
     assert AccumBounds(-1, 2)**oo == AccumBounds(-oo, oo)
     assert AccumBounds(-2, S.Half)**oo == AccumBounds(-oo, oo)
 
-    assert AccumBounds(1, 2)**x == Pow(AccumBounds(1, 2), x, evaluate=False)
+    assert AccumBounds(1, 2)**x == Pow(AccumBounds(1, 2), x)
 
     assert AccumBounds(2, 3)**(-oo) is S.Zero
     assert AccumBounds(0, 2)**(-oo) == AccumBounds(0, oo)
     assert AccumBounds(-1, 2)**(-oo) == AccumBounds(-oo, oo)
 
     assert (tan(x)**sin(2*x)).subs(x, AccumBounds(0, pi/2)) == \
-        Pow(AccumBounds(-oo, oo), AccumBounds(0, 1), evaluate=False)
+        Pow(AccumBounds(-oo, oo), AccumBounds(0, 1))
 
 
 def test_comparison_AccumBounds():
diff --git a/sympy/polys/tests/test_polytools.py b/sympy/polys/tests/test_polytools.py
index 236766afc7..89360b21ee 100644
--- a/sympy/polys/tests/test_polytools.py
+++ b/sympy/polys/tests/test_polytools.py
@@ -3315,7 +3315,3 @@ def test_issue_17988():
     p = poly(x - 1)
     M = Matrix([[poly(x + 1), poly(x + 1)]])
     assert p * M == M * p == Matrix([[poly(x**2 - 1), poly(x**2 - 1)]])
-
-def test_issue_18205():
-    assert cancel((2 + I)*(3 - I)) == 7 + I
-    assert cancel((2 + I)*(2 - I)) == 5
diff --git a/sympy/series/tests/test_limits.py b/sympy/series/tests/test_limits.py
index 93eb22b3d8..a001378e8a 100644
--- a/sympy/series/tests/test_limits.py
+++ b/sympy/series/tests/test_limits.py
@@ -608,3 +608,6 @@ def test_issue_17671():
 
 def test_issue_18306():
     assert limit(sin(sqrt(x))/sqrt(sin(x)), x, 0, '+') == 1
+
+def test_issue_18442():
+    assert limit(tan(x)**(2**(sqrt(pi))), x, oo, dir='-') == AccumBounds(-oo, oo)