# #23: Computing Tangent

Tagged as challenge

Written on 2018-01-30

The tangent of a number $\tan x$ is defined as $\sin x/\cos x$. We can compute tangent by using this definition, or we can make use of a so-called addition formula. The addition formula for tangent is

​ $$\tan (a + b) = \frac{\tan a + \tan b}{1 - \tan a \tan b}.$$

If we wish to compute $\tan x$, then we can compute $\tan(x/2 + x/2)$:

​ $$\tan(\tfrac{x}{2} + \tfrac{x}{2}) = \frac{\tan\tfrac{x}{2} + \tan\tfrac{x}{2}}{1-(\tan\tfrac{x}{2})(\tan\tfrac{x}{2})}=\frac{2\tan\tfrac{x}{2}}{1-(\tan\tfrac{x}{2})^2}$$

We also know something about tangent when the argument is small, namely that tangent is almost linear: $\tan x\approx x$

### Part 1

Write a recursive function tangent using the methods above to compute the tangent of a number. It is not necessary to handle undefined cases (odd multiples of $\pi/2$).

### Part 2

Write an iterative version of tangent called tangent-iter which avoids tree recursion (i.e., doesn't consume any stack space due to recursion).

### Test Cases

Test your code by comparing against your language's built in tangent function. Note that your results may not match precisely, due to the linear approximation ($\tan x \approx x$) and due to other floating point issues.