Prove if $f\colon X\to \mathbb R$, $X ⊂ \mathbb R$ is continuous, then $g(x) :=|f(x)|$, $g\colon X\to\mathbb R$ is also continuous How can I prove that if $f\colon X\to \mathbb R$, $X ⊂ \mathbb R$ is continuous, then $g(x) :=|f(x)|$, $g\colon X\to\mathbb R$ is also continuous?
I tried coming up with a function that respects the stated value, but that didn't help me solve it. Any ideas?
EDIT: I am also curious to know how would I prove that the reverse of that statement is not always valid? So if g is continous, then f is not.
 A: It follows from the inequality $| |a|-|b| | \leq |a-b|$ valid for any two real numbers. Thus if you have for given $\epsilon >0$ that $\delta$ is good for the definition of continuity of $f$, the same $\delta$ is good for $g$. Another way to see this is that $g$ is a composition of two continuous functions, $f$ and $h(x):=|x|$.
A: The reverse triangle inequality implies that
$$
||f(x)|-|f(y)||\leq |f(x)-f(y)|
$$
for all $x,y.$
A: Let $\operatorname{abs}(x)=|x|$. Then $g=\operatorname{abs}\circ f$. Since it is the composition of two continuous functions, $g$ is continuous too.
On the other hand, suppose that$$f(x)=\begin{cases}1&\text{ if }x\in\mathbb Q\\-1&\text{ otherwise}\end{cases}$$(I am assuming that $U=\mathbb R$ here). Then $g=|f|\equiv1$ is continuous, but $f$ is discontinuous everywhere.
A: Hint: you only need to show that $g$ is continuous at its roots, since it's certainly continuous away from its roots. Indeed, by the intermediate value theorem, for every $x$ where $f(x) \not = 0$ there is a ball around $x$ where $f$ takes the same sign on that entire ball, and hence where $g$ is defined either as the continuous $f$ or the continuous $-f$.
