# Integrating $\left|f(x)\right|$ by pulling out $\mathrm{sgn}(f(x))$ from the integral

I tried doing the following integral: $$\int_{0}^{\pi/4}\sqrt{1-\sin2x}\mathrm dx$$. Firstly I completed the square by rewriting $$1$$ as $$\sin^2x+\cos^2x$$ to get the integral revised to this form: $$I=\int_{0}^{\pi/4}\left|\cos x-\sin x\right|\mathrm dx$$ Now clearly I can figure out which trig function is greater on that interval to get rid of that absolute value sign. But let's assume had it been an indefinite integral and I was to find the general antiderivative of the integrand.

I tried putting it into Integral Calculator which pulled out $$\mathrm{sgn}(\cos x-\sin x)$$ and simplified the integral as follows: $$\int \left|\cos x-\sin x\right|\mathrm dx=\mathrm{sgn}(\cos x-\sin x)\int(\cos x-\sin x)\mathrm dx$$

My query is: Can it be done for all integrands contained in absolute value expressions? Does it work for $$\int \left|x\right|\mathrm dx$$ or literally any other expression? I would love to get details as to why it works if it does, maybe in the form of a proof. Many thanks.

Edit: As Gerry Myerson pointed out that this formula: $$\int \left|f'(x)\right|\mathrm dx=f(x)\mathrm{sgn}(f(x))$$ does not always hold in the link shared by Yuito Cheng in the comments. I would like to follow up to the original question.

Under what conditions on $$f'(x)$$ is this formula valid?

• Related (if not a duplicate): What is the indefinite integral of |𝑓(𝑥)|? – YuiTo Cheng Apr 23 at 5:14
• I think my link actually answers your question... – YuiTo Cheng Apr 23 at 5:39
• @YuiToCheng I am looking for an explanation to why this formula works. The link attached seems to account more for the constant term in the sought expression than explaining why the formula is true. :) – Paras Khosla Apr 23 at 5:42
• This formula is false in general; see Gerry Myerson's comment in my link. – YuiTo Cheng Apr 23 at 5:43
• @YuiToCheng So is there any general scheme to be followed while evaluating such indefinite integrals? or When is that formula valid? – Paras Khosla Apr 23 at 5:44