Is there a simpler expression for this piecewise-defined function? As a math-for-fun exercise, I challenged myself to find a globally-defined, everywhere-differentiable antiderivative of $\sqrt{1-\sin(x)}$. Because of the fundamental theorem of calculus, the problem boils down to evaluating the integral
$$f(x)=\int_{-\frac{\pi}{2}}^{x}\sqrt{1-\sin(t)}\text{ }dt$$
After lots of thinking, many laborious calculations and a series of roadblocks, I arrived at this piecewise-defined expression:
$$f(x)=\begin{cases}
4\sqrt{2} \left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor + 2\sqrt{1+\sin(x)} & \text{if } 2\pi \left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor - \frac{\pi}{2}\leq x \leq 2\pi \left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor + \frac{\pi}{2}\\
4\sqrt{2} \left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor +4\sqrt{2} - 2\sqrt{1+\sin(x)} & \text{if } 2\pi \left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor + \frac{\pi}{2}\leq x \leq 2\pi \left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor + \frac{3\pi}{2}
\end{cases}$$
Using knowledge obtained during the solution process, I obtained the (perhaps) simpler expression
$$f(x)=\begin{cases}
4\sqrt{2} \left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor + 2\sqrt{1+\sin(x)} & \text{if } \cos(x)\geq 0\\
4\sqrt{2} \left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor +4\sqrt{2} - 2\sqrt{1+\sin(x)} & \text{if } \cos(x)\leq 0
\end{cases}$$
Naturally, I wondered whether this expression could be simplified further. Maybe there's a non-piecewise expression for $f(x)$? I originally thought about writing
$$4\sqrt{2}\left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor + 4\sqrt{2}=4\sqrt{2} \left( \left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor +1 \right)=4\sqrt{2}\left \lceil \frac{x}{2\pi}+\frac{1}{4} \right \rceil$$
but quickly recognized that this is not valid when $\frac{x}{2\pi}+\frac{1}{4}$ is an integer. I also tried writing
$$f(x)=\begin{cases}
4\sqrt{2} \left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor + 2\text{sgn}(\cos x)\sqrt{1+\sin(x)} & \text{if } \cos(x)\geq 0\\
4\sqrt{2} \left \lfloor \frac{x}{2\pi}+\frac{1}{4} \right \rfloor +4\sqrt{2} + 2\text{sgn}(\cos x)\sqrt{1+\sin(x)} & \text{if } \cos(x)\leq 0
\end{cases}$$
but this fails when $\cos(x)=0$. I don't have any other tricks up my sleeve. Could I get some assistance?
 A: You can verify that
$$ \sqrt{1 - \sin t} = 
\sqrt2 \left\lvert \sin \left(\frac12t - \frac\pi4\right)\right\rvert . $$
This suggests the substitution $u = \frac12t - \frac\pi4,$
$$ \int \sqrt{1 - \sin t} \,\mathrm dt 
 = 2\sqrt2 \int \lvert\sin u\rvert \,\mathrm du
\DeclareMathOperator{\sgn}{sgn}
 = 2\sqrt2 \int \sgn(\sin u) \sin u\,\mathrm du. $$
Within any interval over which $\sin u$ does not change sign
(that is, within the interval $(n\pi, (n+1)\pi)$ where $n$ is an integer),
$$ \int \sgn(\sin u) \sin u\,\mathrm du
 =  \sgn(\sin u) \int \sin u\,\mathrm du
 =  -\sgn(\sin u) \cos u + C. $$
This does not quite work for the closed interval $[n\pi, (n+1)\pi],$
however, because there is a problem with the definition of $\sgn(0).$
Usually we would define $\sgn(0) = 0,$ which would cause
$-\sgn(\sin u) \cos u$ to be $0$ at either end of the interval while the
limit at the left end is $-1$ and the limit at the right is $1.$
We cannot fix this over the entire closed interval $[n\pi, (n+1)\pi],$
but we can at least achieve a continuous antiderivative on the half-open interval
$[n\pi, (n+1)\pi)$ by moving the rest of the curve upward to meet the left endpoint:
$$ \int \sgn(\sin u) \sin u\,\mathrm du
 =  -\sgn(\sin u) (1 + \cos u) + C. $$
This gives us an antiderivative that is piecewise continuous over each interval
$[n\pi, (n+1)\pi)$.
In order to have a continuous antiderivative over a larger interval,
you need to compensate for the fact that $-\sgn(\sin u) \cos u$
jumps between $1$ and $-1$ at every multiple of $\pi$.
We need to add $2$ every time we cross a multiple of $\pi$ in the increasing direction, and not otherwise.
So the missing part of the integral is a step function.
It's practically impossible to get a continuous antiderivative without introducing one of these or inventing some other piecewise definition.
Fortunately, the step function we need is just the floor function scaled as needed along both axes.
We get
$$ \int \sgn(\sin u) \sin u\,\mathrm du
 =  -\sgn(\sin u) (1 + \cos u) + 4 \left\lfloor \frac u{2\pi} \right\rfloor + C. $$
Plug in $u = \frac12t - \frac\pi4$ and multiply the whole thing by $2\sqrt2$
and you have your continuous antiderivative.
A: $$\sqrt{1-\sin x}=\sqrt{\cos^2\frac x2-2\cos\frac x2\sin\frac x2+\sin^2\frac x2}=\left|\cos\frac x2-\sin\frac x2\right|
\\=\sqrt2\left|\sin\left(\frac x2-\frac\pi4\right)\right|$$ and we can focus on the antiderivative of $|\sin t|=\pm\sin t$, where the sign alternates with period $\pi$.
For the antiderivative, we can take $-\cos(t\bmod\pi)$ with a jump of $2$ units at every $k\pi$, to compensate the discontinuity.
$$2\left\lfloor\frac t\pi\right\rfloor-\cos(t\bmod\pi)$$

Don't forget that $t=\dfrac x2-\dfrac\pi4$. If you want, you can emulate the modulo and the floor by elementary transformations of $\tan(\arctan(x))$.
