The notion of equality when considering composition of functions When going through some (very introductory) calculus homework I was given the function $f(x) = \frac{x}{1+x}$ and asked to find the composition $(f\circ f)(x)$ and then find its domain. Substituting, we find that $$(f\circ f)(x)= \frac{\frac{x}{1+x}}{1+\frac{x}{1+x}}$$ The domain is then found by solving $\frac{x}{1+x} = -1$ and to find that the composition is undefined at $-\frac{1}{2}$. It is also, of course, undefined at $-1$. Thus our domain is $\{x \in \mathbb{R} \mid x \neq -\frac{1}{2} $ and $x \neq -1$}. My question comes from noticing that if we take the algebra further we find that $$(f\circ f)(x)= \frac{\frac{x}{1+x}}{1+\frac{x}{1+x}} = \frac{x}{2x+1}$$ The domain is still surely unchanged. However, suppose I never did this problem and for some reason I simply desired to write down the function $\frac{x}{2x+1}$ on a sheet of paper and find its domain. I would find that it is defined for all reals except $-\frac{1}{2}$. (Wolfram alpha also verifies this). This would then imply that $$(f\circ f)(x)= \frac{\frac{x}{1+x}}{1+\frac{x}{1+x}} \neq \frac{x}{2x+1}$$ since the domain of the two functions is unequal. Couldn't we also work backwards from $\frac{x}{2x+1}$ in the following manner? $$\frac{x}{2x+1} = \frac{\frac{x}{1+x}}{\frac{1+2x}{1+x}} = \frac{\frac{x}{1+x}}{\frac{1+x}{1+x} + \frac{x}{1+x}} = \frac{\frac{x}{1+x}}{1 + \frac{x}{1+x}}$$ This is the function we orignally found the domain for. Did I some how just remove a point from the domain by just doing algebraic manipulations?
My guess is perhaps there are two (or more?) notions of equality going on here. One notion would perhaps be the idea of two functions $f$ and $g$ being "formally" equivalent if $f$ can be algebraically manipulated to $g$ and vice versa. The other notion would be the more intuitive one where two functions are equal if they have the same domain and map the elements of the domain to the same points in the codomain.
Thanks.

Note that the notation $(f\circ f)(x)$ gives some intuitive notion that we are going to do $2$ different operations. The first will be to "feed" $x$ to $f$, and then "feed" $f(x)$ to $f$. Indeed we have $(f\circ f)(x) = f(f(x))$. We know that $f$ is not defined for $x = -1$ and therefore the inner $f$ in $f(f(x))$ cannot be "fed" $-1$. However, the outer $f$ is fed values from $f(x)$ and it just so happens that $f(x) = -1 \iff x = -\frac12$.
We can then proceed to write the algebraic manipulations you wrote assuming that $1+x \not= 0, \frac{x}{1+x} \not= -1$ given that otherwise you would be dividing by $0$.
On the other hand, starting from $\frac{1}{2x+1}$ one cannot write 
$$\frac{\frac{x}{1+x}}{\frac{1+2x}{1+x}}$$
if we don't explicitly state that $1+x\not=0$, otherwise you would be dividing by $0$. Therefore one can always do the algebraic manipulations, given that one carries the excluded points along.
Therefore, one finds $f(f(x))$ to be defined for $x\not\in \{-\frac12, -1\}$ and for the points where it is defined we have $f(f(x)) = \frac{1}{2x+1}$. Similarly, working backwards like you did, we get 
$$\frac{\frac{x}{1+x}}{\frac{1+2x}{1+x}} = \frac{\frac{x}{1+x}}{1 + \frac{x}{1+x}}$$
except for the points $x = -\frac12, -1$ because we had to exclude them.
What is more, you are right when you say that two functions are equal if they have the same domain/codomain and if they map the same objects to the same images. Having that in mind, the functions $f(f(x))$ and $\frac1{2x+1}$ are not the same function, unless you restrict the second one to the points where we know $f(f(x))$ is well-defined.
