End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
Math.lgamma
Math.lgamma(x) → [float, -1 or 1]
math.c
View on GitHub ↗
static VALUE
math_lgamma(VALUE unused_obj, VALUE x)
{
double d;
int sign=1;
VALUE v;
d = Get_Double(x);
/* check for domain error */
if (isinf(d)) {
if (signbit(d)) domain_error("lgamma");
return rb_assoc_new(DBL2NUM(HUGE_VAL), INT2FIX(1));
}
if (d == 0.0) {
VALUE vsign = signbit(d) ? INT2FIX(-1) : INT2FIX(+1);
return rb_assoc_new(DBL2NUM(HUGE_VAL), vsign);
}
v = DBL2NUM(lgamma_r(d, &sign));
return rb_assoc_new(v, INT2FIX(sign));
}
Calculates the logarithmic gamma of x and the sign of gamma of x.
Math.lgamma(x) is same as
Example
[Math.log(Math.gamma(x).abs), Math.gamma(x) < 0 ? -1 : 1]
but avoid overflow by Math.gamma(x) for large x.
Example
Math.lgamma(0) #=> [Infinity, 1]