Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method · added in Ruby 4.0
Math.log1p
(::Math::double x) -> ::Float
Math.log1p(x) → float
math.c
View on GitHub ↗
static VALUE
math_log1p(VALUE unused_obj, VALUE x)
{
size_t numbits;
double d = get_double_rshift(x, &numbits);
if (numbits != 0) {
x = rb_big_plus(x, INT2FIX(1));
d = math_log_split(x, &numbits);
/* check for pole error */
if (d == 0.0) return DBL2NUM(-HUGE_VAL);
d = log(d);
d += numbits * M_LN2;
return DBL2NUM(d);
}
domain_check_min(d, -1.0, "log1p");
/* check for pole error */
if (d == -1.0) return DBL2NUM(-HUGE_VAL);
return DBL2NUM(log1p(d)); /* log10(d * 2 ** numbits) */
}
Returns “log(x + 1)”, the base E logarithm of (x + 1).
-
Domain:
[-1, INFINITY]. -
Range:
[-INFINITY, INFINITY].
Examples:
Example
log1p(-1.0) # => -Infinity log1p(0.0) # => 0.0 log1p(E - 1) # => 1.0 log1p(INFINITY) # => Infinity
Ruby version changes
-
4.0
-
Math.log1pandMath.expm1are added. [Feature #21527]
-