Why does $\left(1 + \frac{1}{n}\right)^n$ give vastly different relative errors when $n=252257928$ and $n = 215450934$?

187 Views Asked by At

This expression $\left(1 + \frac{1}{n}\right)^n$ approximates $e^1$.

  • When $n = 252257928$, the relative error, $(e - \text{result})/e$, is $1.740557727387924\mathrm{e-}12$
  • When $n = 215450934$, the relative error is $2.430185813419991\mathrm{e-}08$

But both $n$ are very big numbers. Logically, they should produce similar round-off errors, right? But why is that the resulting round-off errors are so different? What's the logic behind it?

I did the Math on Matlab. It looks that Wolfram Alpha produces different round-off errors, but don't know why.

1

There are 1 best solutions below

3
On BEST ANSWER

The result of $1+\frac1n$ is not exactly representable in floating point, you will get an error $\delta$, the value in memory is $1+\frac1n+δ$ with $|δ|\lessapprox\frac{\mu}2$ (rounding to next) where $\mu\approx 2\cdot 10^{-16}$ is the machine constant.

In the final expression this propagates to \begin{align} \left(1+\frac1n+δ\right)^n &=\exp\left(n\ln\left(1+\frac1n+δ\right)\right)\\ &=\exp\left(1+nδ-\frac n2\left(\frac1n+δ\right)^2+...\right)\\ &=\exp\left(1+nδ-\frac1{2n}-δ-\frac12nδ^2+...\right)\\ &=e\cdot \left(1+(n-1)δ-\frac1{2n}+...\right) \end{align} using $e^{a+b}=e^a(1+b+\frac12b^2+...)$ if $|b|\ll 1$. The leading terms in the relative error are $(n-1)δ$ and $-\frac1{2n}$. The first, random term will reach in its bound the size of the second, theoretical error at around $n\simeq \sqrt{\frac1{\mu}}$ which is about $10^8$. For larger $n$ the random floating point error of maximum size $n\frac{\mu}2$ dominates.

Around $n=10^8$ where both influences balance, it can happen by chance that they are really of equal size but opposing sign, that is, that $δ\approx-\frac1{2n^2}$, so that the resulting error is much smaller than the bounds predict.

  • For the given example $n=252257928$ in the question one gets $2^{52}(\frac1n+\frac1{2n^2})=17853153.999968924$, and thus a very small combined error around $n\cdot 3⋅10^{-5}⋅\mu=1.5⋅10^{-12}$.
  • For the "normal" case example $n=215450934$ this mantissa computation leads to $\frac{2^{52}}n=20903133.459474795$ and thus rounding down by about $δ=-0.5⋅\mu<0$, so that the errors $-0.5/n=-2.32⋅10^{-9}$ and $-0.5⋅\mu⋅(n+1)=-2.39⋅10^{-8}$ reinforce each other.

plot of the relative error