Repository: sympy/sympy

Issue:
Integration is NOT correct!
![126897533-1aa3c024-1fe0-4923-83ef-eab344d1efe9](https://user-images.githubusercontent.com/44011158/128466418-c403aada-cbd0-41d1-a816-fd850465e32d.jpg)

The answer should be π/12



Discussion:

Patch:
diff --git a/sympy/integrals/integrals.py b/sympy/integrals/integrals.py
index 5b6b5548aa..516700ae82 100644
--- a/sympy/integrals/integrals.py
+++ b/sympy/integrals/integrals.py
@@ -602,7 +602,11 @@ def try_meijerg(function, xab):
                             function = ret
                             continue
 
-            if not isinstance(antideriv, Integral) and antideriv is not None:
+            final = hints.get('final', True)
+            # dotit may be iterated but floor terms making atan and acot
+            # continous should only be added in the final round
+            if (final and not isinstance(antideriv, Integral) and
+                antideriv is not None):
                 for atan_term in antideriv.atoms(atan):
                     atan_arg = atan_term.args[0]
                     # Checking `atan_arg` to be linear combination of `tan` or `cot`
@@ -795,7 +799,7 @@ def _do(f, ab):
         return rv
 
     def _eval_integral(self, f, x, meijerg=None, risch=None, manual=None,
-                       heurisch=None, conds='piecewise'):
+                       heurisch=None, conds='piecewise',final=None):
         """
         Calculate the anti-derivative to the function f(x).
 
@@ -1098,6 +1102,7 @@ def _eval_integral(self, f, x, meijerg=None, risch=None, manual=None,
                             # method was set to False already
                             new_eval_kwargs = eval_kwargs
                             new_eval_kwargs["manual"] = False
+                            new_eval_kwargs["final"] = False
                             result = result.func(*[
                                 arg.doit(**new_eval_kwargs) if
                                 arg.has(Integral) else arg


Test patch:
diff --git a/sympy/integrals/tests/test_integrals.py b/sympy/integrals/tests/test_integrals.py
index 6f2ed9ef9b..3bbb9a6a9e 100644
--- a/sympy/integrals/tests/test_integrals.py
+++ b/sympy/integrals/tests/test_integrals.py
@@ -1825,3 +1825,8 @@ def test_issue_21024():
     f = 2*x**2*exp(-4) + 6/x
     F_true = (2*x**3/3 + 6*exp(4)*log(x))*exp(-4)
     assert F_true == integrate(f, x)
+
+
+def test_issue_21831():
+    theta = symbols('theta')
+    assert integrate(cos(3*theta)/(5-4*cos(theta)), (theta, 0, 2*pi)) == pi/12