What are equivalent expressions for the following functions that avoid a loss of significance? 
*

*$\ln ( x + 1 ) - \ln ( x ) $ for large $x$


*$\cos^2 x - \sin^2 x $ for $x \approx \frac{\pi}{4}$



*

*I know in general that when we have subtraction of two very near amounts, it may lead to catastrophic cancelation, therefore I need to change the form of the functions to a new form that doesn't contain the subtraction. However, I have no idea where to start - I know I have to use some trigonometric identities or algebraic manipulation such as:

*$\log \frac{a}{b} = \log(a) - \log(b)$

*$\cos(2x) = \cos^2(x) - \sin^2(x)$
The answers are:

*

*$\ln(\frac{x+1}{x})$ or $\ln(1+\frac{1}{x})$


*$\cos(2x)$
 A: I shall attempt to address the underlying issues.
The expression $$\ln(x+1) - \ln(x)$$ suffers from subtractive cancellation when $x$ is sufficiently large, because $\ln(x+1) \approx \ln(x)$ when $x$ is large. The suggested alternatives, i.e., $$\ln\left(\frac{x+1}{x}\right) \quad \text{or} \quad \ln\left(1 + \frac{1}{x}\right)$$ are both useless when we are using finite precision arithmetic. This is particularly clear in the case of $x=2^{53}$ were $x$ is so large that the IEEE double precision representation of $x+1$ is $x$ and the representation of $1 + \frac{1}{x}$ is $1$. You can easily verify these claims in MATLAB. For example,

x=2^53; y=(1+x)-x;

will produce $y=0$. If $x\ge 2^{53}$ then the suggested alternatives both return $0$ when we use IEEE double precision floating point arithmetic. There is a cure and the name is Taylor. Specifically, we can do a Taylor expansion. It is well-known that $$\ln(1+\epsilon) = \sum_{j=1}^\infty (-1)^{1-j} \frac{\epsilon^j}{j} = \epsilon - \frac{1}{2} \epsilon^2 + \frac{1}{3} \epsilon^3 + \dotsc$$
and a proof can be found in this answer.
In particular, we have $$\ln(x+1) - \ln(x) \approx x^{-1} - \frac{1}{2}x^{-2} + \frac{1}{3} x^{-3} + \dots.$$
There are important issues that arise at this point. Specifically, when can we use the original expression and how many terms should be included in the Taylor expansion? These are good questions for another day.
The expression $$\cos(x)^2 - \sin(x)^2$$ suffers from subtractive cancellation when $x \approx \frac{\pi}{4}$ because $\cos(x)^2 \approx \sin(x)^2$ when $x \approx \frac{\pi}{4}$. The suggested alternative, i.e.,
$$\cos(2x)$$
is useful when we are using finite precision arithmetic. Further transformations are not necessary. The multiplication, i.e., $2x$ will be exact on a binary computer (unless $2x$ exceeds the representational range, i.e., the calculation overflows).
