How calculate $\int_{0}^{\pi}|\sin(x)|dx$ from its indefinite integral I'm having trouble understanding why it is that when I try to calculate  $\int_{0}^{\pi}|\sin(x)|dx$ from it's indefinite integral I seem to get the wrong result of $0$.
Of course $|\sin(x)| = \sin(x) $ on $[0, \pi]$ so I know the result should be $2$. But considering the indefinite integral  
$\int |\sin(x)| = -\cos(x)sgn(\sin(x))$ 
($sgn$ is the sign function) I use some fallacious reasoning to conclude that  
$\int_{0}^{\pi}|\sin(x)|dx = [-\cos(x)sgn(\sin(x))]_{0}^{\pi} = -(-1)(0) -(-1)(0)) = 0$ 
I think the problem is how I use the sign function but as far as I know $sgn(\sin(0)) = sgn(\sin(\pi)) = 0$ and $|\sin(x)| $ is continuous on $[0, \pi]$. What am I doing wrong here?
 A: An indefinite integral, aka antiderivative, is necessarily a continuous function. If you graph the function $-\cos xsgn(\sin x)$, you'll see that it is discontinuous at the multiples of $\pi$.  However, you can make it continuous by adding on the step function $s(x)=2\lfloor x/\pi\rfloor$.  More precisely, we have
$$\int|\sin x|\,dx=C+
\begin{cases}
-\cos xsgn{(\sin x)}+2\lfloor x/\pi\rfloor\quad\text{for }x\not\in\pi\mathbb{Z}\\
2\lfloor x/\pi\rfloor-1\quad\text{for }x\in\pi\mathbb{Z}
\end{cases}$$
where $C$ is an arbitrary constant.
Note, it's easy to see that the derivative of this piecewise-defined function is equal to $|\sin x|$ in each interval $k\pi\lt x\lt(k+1)\pi$ with $k\in\mathbb{Z}$, since $sgn(\sin x)$ and $2\lfloor x/\pi\rfloor$ are constant in each such interval.  It's a good exercise to verify that the derivative exists, and is equal to $0$, at the multiples of $\pi$.
For the given definite integral, we have
$$\int_0^\pi|\sin x|\,dx=(C+2\lfloor\pi/\pi\rfloor-1)-(C+2\lfloor0/\pi\rfloor-1)=(C+2-1)-(C+0-1)=2$$
which agrees, of course, with the simpler calculation
$$\int_0^\pi|\sin x|\,dx=2\int_0^{\pi/2}\sin x\,dx=-2\cos x\big|_0^{\pi/2}=0-(-2)=2$$
Again, the fallacy lay in thinking that the formula $-\cos xsgn(\sin x)$, which defines a discontinuous function, could serve as "the" indefinite integral for $|\sin x|$.
