TITLE: If $|f(x)|$ is a differentiable function, then $f(x)$ is also?
QUESTION [5 upvotes]: If $|f(x)|$ is a differentiable function,  then  $f(x)$ is also a  differentiable function.
Why is this wrong? Can you find a counterexample please?
It seems like a true sentence.

REPLY [18 votes]: Just to give a very extreme example: let $f:\mathbb R \to \mathbb R$ be given by $f(x)=1$ if $x$ is rational and $f(x)=-1$ if $x$ is irrational. Then $|f|$ is constantly $1$, thus differentiable (as many times as you want). But $f$ is not continuous at any point and thus can't be differentiable.

REPLY [7 votes]: What about
$$f(x)=\begin{cases}
1,&\text{if }x\ge 0\\
-1,&\text{if }x<0\;?
\end{cases}$$

REPLY [6 votes]: If $f$ is not continuous, then $\left|f\right|$ may be continuous and even differentiable as pointed out by Brian. Suppose $f$ is continuous at $a$ and $\left|f(x)\right|$ is differentiable at $a$. Then,
$$\lim_{x\to a}\frac{\left|f(x)\right|-\left|f(a)\right|}{x-a}=L\in \mathbb{R}$$
If $f(a)>0$ by continuity, $f(x)>0$ near $a$ and so
$$\lim_{x\to a}\frac{f(x)-f(a)}{x-a}=L\in \mathbb{R}$$
and $f$ is differentiable at $a$. Similarly if $f(a)<0$. 
If $f(a)=0$ then
$$\lim_{x\to a}\frac{\left|f(x)\right|}{x-a}=L\in \mathbb{R}$$
As Robert said in the comments,
$$L=\lim_{x\to a^+}\frac{\left|f(x)\right|}{x-a}\ge 0$$
while
$$L=\lim_{x\to a^-}\frac{\left|f(x)\right|}{x-a}\le 0$$
and so $L=0$. Therefore,
$$\lim_{x\to a^+}\left|\frac{f(x)}{x-a}\right|=0\implies \lim_{x\to a^+}\frac{f(x)}{x-a}=0$$
while
$$\lim_{x\to a^-}\left|\frac{f(x)}{x-a}\right|=0\implies \lim_{x\to a^-}\frac{f(x)}{x-a}=0$$
and so $f$ is differentiable at $0$.
Moral: If $f$ is discontinuous then $\left|f\right|$ may be differentiable.
If $f$ is continuous and $\left|f\right|$ is differentiable then $f$ is differentiable as well