Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Base-n logarithm in Ruby

This one seems like an easy one, but I'm having trouble calculating log (Base 5) in Ruby.

Clearly the standard base-10 log works fine:

>> value = Math::log(234504)
=> 12.3652279242923

But in my project I need to use Base 5. According to the ruby docs (http://www.ruby-doc.org/core/classes/Math.html#M001473) it seems I should be able to do this:

Math.log(num,base) → float

>> value = Math::log(234504, 5)
ArgumentError: wrong number of arguments (2 for 1)
    from (irb):203:in `log'
    from (irb):203
    from :0

Which it doesn't like. Anyone know how to calculate logs in base-n in ruby on rails?

Thanks!

like image 301
Scott Allen Avatar asked Jan 23 '11 22:01

Scott Allen


1 Answers

I'll check Ruby function but don't forget your basics: alt text

Prior to Ruby 1.9:

> Math::log(234504) / Math::log(5)
=> 7.682948083154834

In Ruby 1.9 and later, the second argument was introduced:

> Math::log(234504, 5)
=> 7.682948083154834
like image 135
apneadiving Avatar answered Sep 19 '22 12:09

apneadiving