Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
Math.log2
Math.log2(x) → float
math.c
View on GitHub ↗
static VALUE
math_log2(VALUE unused_obj, VALUE x)
{
size_t numbits;
double d = get_double_rshift(x, &numbits);
domain_check_min(d, 0.0, "log2");
/* check for pole error */
if (d == 0.0) return DBL2NUM(-HUGE_VAL);
return DBL2NUM(log2(d) + numbits); /* log2(d * 2 ** numbits) */
}
Returns the base 2 logarithm of x.
-
Domain:
[0, INFINITY]. -
Range:
[-INFINITY, INFINITY].
Examples:
Example
log2(0.0) # => -Infinity log2(1.0) # => 0.0 log2(2.0) # => 1.0 log2(INFINITY) # => Infinity