MATLAB: Difference in MATLAB function evaluation

approximationerrorgammagamma_function

According to the properties of gamma function, for any x > 0
But when I try to use it in MATLAB, it shows a very large error
gamma(150) - 149*gamma(149)
ans =
-1.5240e+247
Can anybody explain the reason for this? I need it for an approximation algorithm I am trying to develop.
Thanks in advance.

Best Answer

For your use case, 150! is a very large number so the numerical precision issue starts to kick in. I'd suggest you to use gammaln instead, for example, you can do
gammaln(150)-(log(149)+gammaln(149))
and the result is indeed 0.
HTH.