Find the value : $\int_{-\pi}^{\pi} \dfrac{\cos^2(x)}{1+a^x}$ $$\int_{-\pi}^{\pi} \dfrac{\cos^2(x)}{1+a^x} = \int_{-\pi}^{\pi} \dfrac{a^x\cos^2(x)}{1+a^x}$$
I tried using by parts, but in the end get $I = 0$. This is not the first time I encounter this mistake. Please show me a way.
 A: Another way of solving (a harder way):
$$\mathcal{I}(\text{a})=\int_{-\pi}^\pi\frac{\cos^2(x)}{1+\text{a}^x}\space\text{d}x=\frac{1}{2}\left\{\int_{-\pi}^\pi\frac{1}{1+\text{a}^x}\space\text{d}x+\int_{-\pi}^\pi\frac{\cos(2x)}{1+\text{a}^x}\space\text{d}x\right\}$$
Use:


*

*Substitute $u=\text{a}^x$ and $\text{d}u=\text{a}^x\ln(\text{a})\space\text{d}x$:
$$\int_{-\pi}^\pi\frac{1}{1+\text{a}^x}\space\text{d}x=\frac{1}{\ln(\text{a})}\left\{\int_{\text{a}^{-\pi}}^{\text{a}^\pi}\frac{1}{u}\space\text{d}u-\int_{\text{a}^{-\pi}}^{\text{a}^\pi}\frac{1}{1+u}\space\text{d}u\right\}$$

*$$\int_{\text{a}^{-\pi}}^{\text{a}^\pi}\frac{1}{u}\space\text{d}u=\left[\ln\left|u\right|\right]_{\text{a}^{-\pi}}^{\text{a}^\pi}=\ln\left|\text{a}^\pi\right|-\ln\left|\text{a}^{-\pi}\right|$$

*Substitute $\text{s}=1+u$ and $\text{d}\text{s}=\text{d}u$
$$\int_{\text{a}^{-\pi}}^{\text{a}^\pi}\frac{1}{1+u}\space\text{d}u=\int_{1+\text{a}^{-\pi}}^{1+\text{a}^\pi}\frac{1}{\text{s}}\space\text{d}\text{s}=\left[\ln\left|\text{s}\right|\right]_{1+\text{a}^{-\pi}}^{1+\text{a}^\pi}=\ln\left|1+\text{a}^\pi\right|-\ln\left|1+\text{a}^{-\pi}\right|$$

*Substitute $\text{p}=2x$ and $\text{d}\text{p}=2\space\text{d}x$:
$$\int_{-\pi}^\pi\frac{\cos(2x)}{1+\text{a}^x}\space\text{d}x=\frac{1}{2}\int_{-2\pi}^{2\pi}\frac{\cos(\text{p})}{1+\text{a}^{\frac{\text{p}}{2}}}\space\text{d}\text{p}=0$$


So:
$$\mathcal{I}(\text{a})=\int_{-\pi}^\pi\frac{\cos^2(x)}{1+\text{a}^x}\space\text{d}x=\frac{\ln\left|\text{a}^\pi\right|-\ln\left|\text{a}^{-\pi}\right|+\ln\left|1+\text{a}^{-\pi}\right|-\ln\left|1+\text{a}^\pi\right|}{2\ln(\text{a})}=\frac{\pi}{2}\cdot\frac{\ln\left|\text{a}\right|}{\ln(\text{a})}$$
A: Let $$I = \int^{\pi}_{-\pi}\frac{\cos^2 x}{1+a^x}dx = \int^{\pi}_{-\pi}\frac{a^x\cos^2 x}{1+a^x}dx$$(As you have already written)
So $$2I = \int^{\pi}_{-\pi}\frac{(1+a^x)\cos^2 x}{1+a^x}dx = \int^{\pi}_{-\pi}\cos^2 xdx=2\int^{\pi}_{0}\cos^2 xdx$$
(Bcz $f(x)$ is even function)
So $$2I = \int^{\pi}_{0}(1+\cos 2x)dx = \pi$$
So $$I = \frac{\pi}{2}$$
