TITLE: One-Way Inverse
QUESTION [10 upvotes]: My Algebra $2$ teacher stressed the fact that when you find the inverse $g$ of a function $f$, you must not only check that
$$f \circ g=\operatorname{id}$$
but you must also check that
$$g \circ f=\operatorname{id}$$
For example, if 
$$f(x)=x^2$$
then
$$g(x)=\sqrt{x}$$
is not its inverse, because
$$f(g(x))=\sqrt{x^2}=|x|\ne x$$
However, I feel that this is minor... $|x|$ is equal to $x$ half of the time (if $x$ is real) and the other half of the time, it is just $-x$.
Can anyone think of an example of two functions $f$ and $g$ such that
$$f \circ g=\operatorname{id}$$
but, when composed in the other order, the result is something totally wacky that is almost never equal to $\operatorname{id}$?

REPLY [9 votes]: $$
f(x) = \dfrac{x}{1+|x|} ,
\hspace{0.1 in} g(x) = \begin{cases} \frac{x}{1-|x|}\, & |x|<1 \\ 0 & |x|\ge 1 \end{cases}\,.
$$
$g\circ f = \text{id}$ but $f\circ g$ is not the identity function.

REPLY [5 votes]: The existence of $g$ such that $f\circ g=\operatorname{id} $ means (is equivalent to) $f$ is surjective, such that $g\circ f=\operatorname{id} $ means $f$ is injective. Why do you want being surjective is equivalent to being injective in general?
For instance, the first projection $\;\begin{aligned}[t]\mathbf R^2&\longrightarrow\mathbf R\\(x,y)&\longmapsto x\end{aligned}$ has a right inverse, which is the canonical injection
$\begin{aligned}[t]\mathbf R&\longrightarrow\mathbf R^2\\x&\longmapsto (x,0)\end{aligned}$, and the composition of both is indeed the identity of $\mathbf R$. But the composition the other way yields the map $(x,y)\longmapsto (x,0)$.