function value = r8_factorial_stirling ( n )

%*****************************************************************************80
%
%% r8_factorial_stirling() computes Stirling's approximation to N factorial.
%
%  Discussion:
%
%    N! = Product ( 1 <= I <= N ) I
%
%    Stirling ( N ) = sqrt ( 2 * PI * N ) * ( N / E )^N * E^(1/(12*N) )
%
%    This routine returns the raw approximation for all nonnegative
%    values of N.  If N is less than 0, the value is returned as 0,
%    and if N is 0, the value of 1 is returned.  In all other cases,
%    Stirling's formula is used.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    07 April 2016
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the argument of the function.
%
%  Output:
%
%    real VALUE, an approximation to N!.
%
  r8_e = 2.71828182845904523;

  if ( n < 0 )

    value = 0.0;

  elseif ( n == 0 )

    value = 1.0;

  else

    value = sqrt ( 2.0 * pi * n ) * ( n / r8_e ) ^ n * exp ( 1.0 / ( 12 * n ) );

  end

  return
end
