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

  2. $\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:

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

  2. $\cos(2x)$

TShiong
  • 1,257

1 Answers1

1

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).

Carl Christian
  • 12,583
  • 1
  • 14
  • 37
  • some math libraries have a log1p(x) function that evaluates log(1+x) without loss of precision when x is small, then log(1+1/x) evaluated using log1p(1/x) should be accurate - I guess it is implemented with the Taylor series internally – Claude Feb 07 '22 at 17:43
  • @Claude Quite so. I find it challenging to select the right level of detail in cases such as this where we know nothing about the background of the OP. – Carl Christian Feb 07 '22 at 18:52