Morton Goldberg
2/8/2007 5:00:00 AM
On Feb 7, 2007, at 9:00 PM, Eric I. wrote:
> On Feb 7, 7:08 pm, Morton Goldberg <m_goldb...@ameritech.net> wrote:
>> Well, I can find examples with the base first or second. Perhaps for
>> Ruby it would most idiomatic to define log as follows
>>
>> def Math.log(x, b=Math::E); ... ; end
>>
>> as the core method with log10 and log2 handled (for performance) as
>> special cases within this method. Given that, I would still want Math
>> to implement log10 and log2 methods for convenience.
>
> Math.log calls the log in the standard math library, which I suspect
> implements the natural log calculation pretty efficiently. If you
> make the second parameter the base, you'll either have to check it
> against Math::E and treat it as a special case or do two log
> calculations for the natural log case. I don't like either of those
> options.
If there's too much of a performance penalty for this, I would have
no problem with the general case being implemented under a different
name, and I would suggest "logb" -- that is,
def Math.logb(x, b=Math::E); ... ; end
My main point is that I think the general case should default its
base to Math::E. If we were to end up with Math.log (alias Math.ln),
Math.log10, Math.log2, and Math.logb (the general case) that would
be fine.
Regards, Morton