method

log

v1_9_3_392 - Show latest stable - Class: Math
log(p1, p2 = v2)
public

Returns the natural logarithm of numeric. If additional second argument is given, it will be the base of logarithm.

Math.log(1)          #=> 0.0
Math.log(Math::E)    #=> 1.0
Math.log(Math::E**3) #=> 3.0
Math.log(12,3)       #=> 2.2618595071429146

1Note

Any base logarithm

szeryf ยท Jul 13, 20091 thank

Using basic arithmetic you can get logarithm with any base:

def log_with_base base, num
Math.log(num) / Math.log(base)
end

Examples:

>> log_with_base 2, 10
=> 3.32192809488736
>> log_with_base 2, 2
=> 1.0
>> log_with_base 2, 4
=> 2.0
>> log_with_base 2, 16
=> 4.0
>> log_with_base 4, 16
=> 2.0