spinoso_math

Function gamma

Source
pub fn gamma(value: f64) -> Result<f64, DomainError>
Expand description

Calculates the gamma function of the given value.

Note that gamma(n) is same as fact(n-1) for integer n > 0. However gamma(n) returns float and can be an approximation.

§Examples

use spinoso_math as math;
assert_eq!(math::gamma(1.0), Ok(1.0));
assert_eq!(math::gamma(2.0), Ok(1.0));
assert_eq!(math::gamma(3.0), Ok(2.0));
assert_eq!(math::gamma(4.0), Ok(6.0));
assert_eq!(math::gamma(5.0), Ok(24.0));
assert_eq!(math::gamma(20.0), Ok(1.21645100408832e+17));

assert!(math::gamma(-15.0).is_err());
assert!(matches!(math::gamma(-15.1), Ok(result) if (result - 5.9086389724319095e-12).abs() < f64::EPSILON));

assert!(math::gamma(f64::NEG_INFINITY).is_err());
assert_eq!(math::gamma(f64::INFINITY), Ok(f64::INFINITY));

§Errors

If the given value is negative, a domain error is returned.