# Example of a function $f$ which is nowhere continuous but $|f|$ should be continuous at all points [duplicate]

So I had an exam today and one of the questions were: Give an example of a function $f$ which is nowhere continuous but $|f|$ should be continuous at all points. At first I had no idea how to do it then I came up with this, even though I know it's wrong:

$$f(x)= \begin {cases} \ \sqrt{x}&\text{if }x <0\\ \sqrt{-x}\ &\text{if }x > 0\\ \end {cases}$$

Like I said I know it's wrong..but can someone give me an example of this? Thanks.

## marked as duplicate by David Mitra, user1337, Shuchang, Cameron Buie, Eric NaslundDec 17 '13 at 1:03

Let $$f(x)=\begin{cases}1,&\text{if }x\in\Bbb Q\\-1,&\text{if }x\in\Bbb R\setminus\Bbb Q\;.\end{cases}$$