1. The substitution
Put x=tanθ, so θ=tan−1x. Then
1−x22x=1−tan2θ2tanθ=tan2θ
which is the double-angle formula for the tangent. So
y=tan−1(tan2θ)
2. When the simplification holds
tan−1(tanα)=α only when α lies in the principal branch (−2π,2π). Here α=2θ, so we need
−2π<2θ<2π⟺−4π<θ<4π⟺−1<x<1
So y=2tan−1x is correct precisely for ∣x∣<1 — which is why the programmer's tests passed near the origin and failed for large x.
3. The case x>1
If x>1 then θ∈(4π,2π) and so 2θ∈(2π,π), which is outside the principal branch. Subtracting π brings it back inside, and tan has period π, so
y=2θ−π=2tan−1x−π
(Similarly y=2tan−1x+π for x<−1.)
4. The derivative
For ∣x∣<1, differentiating y=2tan−1x:
dxdy=1+x22
For x>1, differentiating y=2tan−1x−π gives the same thing, since the derivative of the constant π is zero:
dxdy=1+x22
So on every interval of the domain,
dxdy=1+x22
5. Why the derivative does not notice
The three expressions for y differ only by the constants 0, −π and +π on the three intervals (−∞,−1), (−1,1) and (1,∞). Differentiation destroys constants, so all three give 1+x22.
What the constants do affect is the function itself: y jumps by π as x crosses 1, where the original expression is undefined because 1−x2=0. A derivative computed blindly as 1+x22 therefore describes the slope correctly everywhere it exists, but says nothing about the jump — which is precisely the bug the programmer saw. The moral for inverse-trigonometric work: check the range before cancelling tan−1tan, even though the derivative may forgive you.