src/pylib/Lib/math_impl/patch/gamma

Source   Edit  

Stirling's formula for the gamma function gamma(x) = sqrt(2 pi) x^(x-.5) exp(-x) ( 1 + 1/x P(1/x) )

For float32: .028 < 1/x < .1 relative error < 1.9e-11

Procs

func gamma[F: SomeFloat](x: F): F
CPython math.gamma-like, with error message more detailed.

Example:

template chkValueErr(arg) =
  doAssertRaises ValueError: discard gamma arg
chkValueErr NegInf
chkValueErr 0.0
# Currently +-0.0 result is not consistent with CPython;
# assert NegInf == 1.0/gamma(-180.5)
Source   Edit  
func gamma[T: SomeFloat](x: T; res: var T): GammaError
a more error friendly version of gamma Source   Edit  
func rGamma[F: SomeFloat](x: F): F {....raises: [].}
behaviors like R's gamma except for this without any warning.

Example:

from std/math import isNaN
assert isNaN rgamma NegInf
assert Inf == 1/rgamma(-180.5)  # never returns -0.0
Source   Edit  
func stdlibJsGamma[F: SomeFloat](x: F): F {....raises: [].}
behaviors like @stdlib-js/gamma.js Source   Edit  

Templates

template MAX_STIRLING[F](_: typedesc[F]): F
Source   Edit  
template UseSmallApprox[F](_: typedesc[F]): F
Source   Edit