Could anyone help/guide me towards properly analyzing the overflow as from the value of n = 10? The small program below calculates the absolute and relative errors in the Stirling's approximation $$n! = \sqrt{2\pi n}\left(\frac{n}{e}\right)^n$$
For n = 1,2,...10.
Program StirlingTable
implicit none
integer :: n, nf
real :: stirling, abs_error, pi, rel_error

pi = acos(-1.0) ! definition of the variable pi
nf = 1       ! starting value for n!

do n = 1, 10
    nf = nf*n
    stirling = sqrt(2.0*pi*n)*exp(-real(n))*((n)**n)
    abs_error = abs(nf - stirling)
    rel_error = abs_error/nf
    write(*,*) n, nf, stirling, &
                             abs_error, rel_error


end do
         read(*,*)

End Program StirlingTable
When compiling, the results give

Could anyone help me analyze the reasons behind why the absolute error increases and the relative error decreases with the increase in n, especially within the context of the code.
That is, which part of the code would be responsible for the overflow error.
Thank you
 A: Stirling approximation states that
$$
n!  \sim \sqrt{2\pi n} \left( \frac{n}{e} \right)^n,
$$
meaning
$$
\lim_{n \to \infty} \frac{n!}{\sqrt{2\pi n} \left( \frac{n}{e} \right)^n} = 1.
$$
This does not mean that the following
$$
\lim_{n \to \infty} \left( n!  - \sqrt{2\pi n} \left( \frac{n}{e} \right)^n\right) = 0
$$
is true.
To feel how this can happen consider two simpler equivalent sequences, for instance
$$
n^2 + n\sim n^2.
$$
The absolute error is
$$
(n^2 + n) - n^2 = n \to \infty,
$$
but the relative error is
$$
\frac{(n^2 + n) - n^2}{n^2} = \frac{1}{n} \to 0.
$$
A: Let
$$A=n! \qquad \text{and} \qquad B=\sqrt{2\pi n} \left( \frac{n}{e} \right)^n$$
Using Sirling approximation, we have $$\log \left(\frac{A}{B}\right) = \frac{1}{12 n}+O\left(\frac{1}{n^3}\right)\implies \frac{A}{B}=1+ \frac{1}{12 n}+O\left(\frac{1}{n^2}\right) $$ Then
$$A-B\sim\frac{B}{12 n}=\frac 16 \sqrt{\frac{\pi }{2n}} \left( \frac{n}{e} \right)^n$$ increases very fast (just as $n \log(n)$) but
$$\frac{A-B}{B}\sim \frac{1}{12 n}$$ decreases.
