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