# How does integrating over absolute values work with definite integrals?

I have $\int_0^\pi | \sin(x/2) | \, dx$, and according to Wolfram Alpha, the indefinite integral is:

$$-2\cos(x/2)\operatorname{sgn}(\sin(x/2)) + C$$

but the definite integral above evaluates to $2$, while substituting the limits of integration into the indefinite integral gives: $-2\cos(\pi/2)\operatorname{sgn}(\sin(\pi/2)) + 2\cos(0)\operatorname{sgn}(\sin(0)) = 0$.

Is the expression Wolfram gave for the indefinite integral wrong, or have I stumbled somehow onto an integral that doesn't satisfy: $\left.\int f(x)\,dx \right|_a - \int f(x)\,dx \rvert_b = \int_a^b f(x) \, dx$, with $|_a$ meaning "evaluated at $a$"? (which I thought was the definition of an indefinite integral)

This works if instead of defining $\operatorname{sgn}(x)$ to be $-1$ if $x$ is negative, $0$ if $x$ is zero, and $1$ if $x$ is positive, we define $\operatorname{sgn}(x)$ to be $1$ if $x$ is zero instead of $0$. Is this the true definition of the indefinite integral, and will this work in general for integrals of absolute values?

Interesting find! I have to wonder if Wolfram does define $\operatorname{sgn}(0)=1$, because otherwise you are correct, and Wolfram is wrong. In general there won't be a nice looking (closed) answer for integrals like this. In your case, because sine is periodic and will take on negative values periodically, which will require careful work with the absolute value function. But here is a general technique that will work nicely for a definite integral of a function that is Riemann integrable. Consider $$\int_{a}^b\left|f(x)\right|dx$$ where $f$ is Riemann integrable and $f$ changes sign over $[a,b]$ a finite number of times. We can break up the interval of integration $$[a,b] = [x_0,x_1] \cup [x_1,x_2] \cup \ldots \cup [x_{n-1},x_n]$$ where $a = x_0, b = x_n$ and $f(x_i)=0$ for all $i \in \{1,2,\ldots n-1 \}$ In this way, $f$ is either greater than or equal to zero on $[x_{i-1},x_i]$ or less than or equal to zero on $[x_{i-1},x_i]$, but never obtains both positive and negative values on the same interval. Then take the sum $$\sum_{i=1}^{n}\left(\int_{x_{i-1}}^{x_i}s_i\cdot f(x)dx\right)$$ with $$s_i = \begin{cases} 1 &\mbox{if } f\left(\frac{x_{i-1}+x_i}{2} \right) \geq 0 \\ -1 & \mbox{if } f\left(\frac{x_{i-1}+x_i}{2} \right) < 0 \end{cases}$$ Note that $s_i$ assigns a sign to the integral of $f$ over $[x_{i-1},x_i]$ We should find that $$\int_{a}^b\left|f(x)\right|dx = \sum_{i=1}^{n}\left(\int_{x_{i-1}}^{x_i}s_i\cdot f(x)dx\right)$$ which is intuitively true, but a rigorous proof wouldn't hurt. This might look more complicated than it is worth, but let's now calculate your integral! First we need to find values of $x$ such that $\sin(x/2)=0$. Clearly $x=0$ and $x = 2\pi$ work. But $x = 2\pi$ is outside the interval of integration so we don't need to break up the interval $[0,\pi]$. This means $n=1$. Now we can get $s_1$. Since $$\sin\left(\frac{\left(\frac{0+\pi}{2}\right)}{2}\right)=\sin\left(\frac{\pi}{4}\right) = \frac{\sqrt{2}}{2} \geq 0$$ we know $s_1 = 1$. Putting this all together, we evaluate $$\int_{0}^\pi\left|\sin\left(\frac{x}{2}\right)\right|dx = \sum_{i=1}^{1}\left(\int_{x_{i-1}}^{x_i}s_i\cdot \sin\left(\frac{x}{2}\right)dx\right) \\ = \int_{0}^{\pi}1\cdot \sin\left(\frac{x}{2}\right)dx \\= 2$$ So this result wasn't too bad, because really all we had to do was drop the absolute value sign from the original integral. This process will always work for definite integrals. I imagine it would get tedious for $n >3$ and problems would occur if $f$ changed signs infinitely many times over $[a,b]$. But as long as only finitely many sign changes occur, integrals of this type are certainly possible to calculate by hand or machine. I would be weary of any indefinite integral of this type unless you know $f(x) \geq 0$ for all $x \in \operatorname{domain}\{f\}$
The indefinite integral given can't be right -- it ought to go to $+\infty$ for $x\to\infty$, but the expression you give is clearly periodic.
It looks like Wolfram Alpha produces an expression for the indefinite integral that only works on intervals where the sign of $\sin(x/2)$ is constant.
When $x\in [0,\pi]$, we have $x/2 \in [0,\pi/2]$, and so $\sin(\frac{x}{2})\ge 0$. This implies $$\int\limits_0^\pi {\left| {\sin \left( {\frac{x} {2}} \right)} \right|dx} = \int\limits_0^\pi {\sin \left( {\frac{x} {2}} \right)dx} = \left[ { - 2\cos \left( {\frac{x} {2}} \right)} \right]_{x = 0}^{x = \pi } = 2.$$