Discrepancy in differentiating: $y=\tan^{-1}\left(\frac{\sqrt{1+\sin{x}}+\sqrt{1-\sin{x}}}{\sqrt{1+\sin{x}}-\sqrt{1-\sin{x}}}\right)$ $$y=\tan^{-1}\left(\frac{\sqrt{1+\sin{x}}+\sqrt{1-\sin{x}}}{\sqrt{1+\sin{x}}-\sqrt{1-\sin{x}}}\right)$$
$$\sqrt{1+\sin{x}}=\sin{\frac{x}{2}}+\cos{\frac{x}{2}}$$
$$\sqrt{1-\sin{x}}=\sin{\frac{x}{2}}-\cos{\frac{x}{2}}$$
$$y=\tan^{-1}\left(\tan{\frac{x}{2}}\right)$$Differentiating, we get: $$\frac{dy}{dx}=\frac{1}{2}$$
But taking: $$\sqrt{1-\sin{x}}=\cos{\frac{x}{2}}-\sin{\frac{x}{2}}$$
$$y=\tan^{-1}\left(\cot{\frac{x}{2}}\right)$$
Differentiating, we get: $$\frac{dy}{dx}=\frac{-1}{2}$$
I figured that I needed to use absolute value for the simplification of $\sqrt{1-\sin{x}}$, i.e. $\sqrt{1-\sin{x}}=\left|\sin{\frac{x}{2}}-\cos{\frac{x}{2}}\right|$. 
Subsequently, I put the below functions into Wolfram Alpha's input box to differentiate: $$y_1=\tan^{-1}\left(\frac{\sqrt{1+\sin{x}}+\sqrt{1-\sin{x}}}{\sqrt{1+\sin{x}}-\sqrt{1-\sin{x}}}\right)$$
And
$$y_2=\tan^{-1}\left(\frac{\left|\sin{\frac{x}{2}}+\cos{\frac{x}{2}}\right|+\left|\sin{\frac{x}{2}}-\cos{\frac{x}{2}}\right|}{\left|\sin{\frac{x}{2}}+\cos{\frac{x}{2}}\right|-\left|\sin{\frac{x}{2}}-\cos{\frac{x}{2}}\right|}\right)$$
The answers should ideally match, but they dont. $\frac{dy_1}{dx}=\frac{-1}{2}\neq\frac{dy_2}{dx}$
Why don't they?
 A: At $x = 0$, $\sqrt{1-\sin x} = 1$, this means $\sqrt{1-\sin x} = \cos\frac{x}{2} - \sin\frac{x}{2}$ instead of $\sin\frac{x}{2} - \cos\frac{x}{2}$.
For $x \approx 0$, more precisely, $x \in (-\frac{\pi}{2},\frac{\pi}{2})$, 
we have $\frac{dy}{dx} = -\frac12$.
Notice $\sqrt{1-\sin x}$ is not smooth at the boundary of $(-\frac{\pi}{2},\frac{\pi}{2})$ and it develop cusps there.
When one start increasing $x$ from $0$ to beyond $\pi/2$, above formula for $\sqrt{1-\sin x}$ start to fail at $x = \frac{\pi}{2}$. Instead, we have $\sqrt{1-\sin x} = \sin\frac{x}{2} - \cos\frac{x}{2}$ for $x \in ( \frac{\pi}{2},\pi )$. This means $\frac{dy}{dx} = \frac12$ over that interval.
In general, $y(x)$ is a piecewise linear function with jump discontinuities at
integer multiple of $\pi$. Its derivatives has jump discontinuities at
half-integer mulitples of $\pi$. To summarize,
$$\frac{dy(x)}{dx} = 
\begin{cases}
\text{undefined}, & \frac{2x}{\pi} \text{ is a integer}\\
-\frac12, & {\rm round}(\frac{x}{\pi}) \text{ is even}\\
+\frac12, & {\rm round}(\frac{x}{\pi}) \text{ is odd}.
\end{cases}
$$
In certain sense, both answer $\pm \frac12$ are right and both are wrong. Both are right because each of them are valid over some subsets of $\mathbb{R}$. Both are wrong because $\frac{dy}{dx}$ is not a constant function.
A: $$\dfrac{\sqrt{1+\sin x}+\sqrt{1-\sin x}}{\sqrt{1+\sin x}-\sqrt{1-\sin x}}=\dfrac{1+\sqrt{\dfrac{1-\sin x}{1+\sin x}}}{1-\sqrt{\dfrac{1-\sin x}{1+\sin x}}}$$
Now $\dfrac{1-\sin x}{1+\sin x}=\left(\dfrac{\cos\dfrac x2-\sin\dfrac x2}{\cos\dfrac x2+\sin\dfrac x2}\right)^2=\left(\dfrac{1-\tan\dfrac x2}{1+\tan\dfrac x2}\right)^2=\tan^2\left(\dfrac\pi4-\dfrac x2\right)$
We know for real $a,\sqrt{a^2}=|a|=\begin{cases}a &\mbox{if }a\ge0\\ 
-a & \mbox{if }a<0\end{cases}$
For $\tan\left(\dfrac\pi4-\dfrac x2\right)\ge0,$
$$\tan^{-1}\dfrac{\sqrt{1+\sin x}+\sqrt{1-\sin x}}{\sqrt{1+\sin x}-\sqrt{1-\sin x}}=\tan^{-1}\dfrac{1+\tan\left(\dfrac\pi4-\dfrac x2\right)}{1-\tan\left(\dfrac\pi4-\dfrac x2\right)}=\tan^{-1}\tan\left(\dfrac\pi4+\dfrac\pi4-\dfrac x2\right)=n\pi+\dfrac\pi4+\dfrac\pi4-\dfrac x2$$
where $n$ is an arbitrary constant such that $-\dfrac\pi2\le y\le\dfrac\pi2$
What if $\tan\left(\dfrac\pi4-\dfrac x2\right)<0?$
A: $$\dfrac{\sqrt{1+\sin x}+\sqrt{1-\sin x}}{\sqrt{1+\sin x}-\sqrt{1-\sin x}}=\dfrac{(\sqrt{1+\sin x}+\sqrt{1-\sin x})^2}{1+\sin x-(1-\sin x)}=\dfrac{1+|\cos x|}{\sin x}$$
Case$\#1:$ If $\cos x\ge0, |\cos x|=+\cos x$
Using $\cos2A=2\cos^2A-1,\sin2A=2\sin A\cos A$
$\tan^{-1}\dfrac{\sqrt{1+\sin x}+\sqrt{1-\sin x}}{\sqrt{1+\sin x}-\sqrt{1-\sin x}}=\tan^{-1}\dfrac{1+\cos x}{\sin x}=\tan^{-1}\cot\dfrac x2=\tan^{-1}\tan\left(\dfrac{\pi-x}2\right)$
$y=m\pi+\dfrac{\pi-x}2$ where $m$ is an arbitrary constant such that $-\dfrac\pi2\le y\le\dfrac\pi2$
$$\dfrac{dy}{dx}=?$$
Case$\#2:$ If $\cos x<0, |\cos x|=-\cos x$
Please carry on
