Repository: sympy/sympy

Issue:
RuntimeError in subs
`((-sin(1/x)**2 + 1)).subs(x,0)` raises `RuntimeError: maximum recursion depth exceeded`

Discussion:

Patch:
diff --git a/sympy/core/power.py b/sympy/core/power.py
index bd4bb7ae15..db3aa08564 100644
--- a/sympy/core/power.py
+++ b/sympy/core/power.py
@@ -1087,7 +1087,8 @@ def as_real_imag(self, deep=True, **hints):
                 if re.is_Number and im.is_Number:
                     # We can be more efficient in this case
                     expr = expand_multinomial(self.base**exp)
-                    return expr.as_real_imag()
+                    if expr != self:
+                        return expr.as_real_imag()
 
                 expr = poly(
                     (a + b)**exp)  # a = re, b = im; expr = (a + b*I)**exp
@@ -1097,7 +1098,8 @@ def as_real_imag(self, deep=True, **hints):
                 if re.is_Number and im.is_Number:
                     # We can be more efficient in this case
                     expr = expand_multinomial((re + im*S.ImaginaryUnit)**-exp)
-                    return expr.as_real_imag()
+                    if expr != self:
+                        return expr.as_real_imag()
 
                 expr = poly((a + b)**-exp)
 


Test patch:
diff --git a/sympy/core/tests/test_arit.py b/sympy/core/tests/test_arit.py
index 110125aaa0..3bf9be5bd5 100644
--- a/sympy/core/tests/test_arit.py
+++ b/sympy/core/tests/test_arit.py
@@ -1981,3 +1981,7 @@ def test_issue_8247_8354():
 def test_Add_is_zero():
     x, y = symbols('x y', zero=True)
     assert (x + y).is_zero
+
+
+def test_issue_14392():
+    assert (sin(zoo)**2).as_real_imag() == (nan, nan)