In a world of humans and finance, it’s quite common to need base-10
logarithms. (Yay for Math.log10!)
In a world of computers, it’s also quite common to need base-2
logarithms. Ruby only gives you ‘natural’ logarithms in the form of
It has probably been more than 5 times that I’ve had to look up the
formula to discover than
log-base-n(x) == log-base-whatever(x) / log-base-whatever(n)
I was reminded by it when Martin had to write his own log2 function to
answer my previous question.
Is there any objection to me proposing to add both:
Math.log2(x) - base-2 logarithm
Math.logn(x,n) - base-n logarithm
directly to the core of Ruby?
Ideally they’d be implemented in C, but of course the simple Ruby
implementation is just:
def Math.log2( x ); Math.log( x ) / Math.log( 2 ); end
def Math.logn( x, n ); Math.log( x ) / Math.log( n ); end
Since they’re so simple, I’m almost hesitant to ask for them in
core…except that they’ve cropped up for me again and again, and it
always takes me a while to hunt them down. Languages like Lua thrive
on simplicity of implementation. I think Ruby thrives on the richness
of the immediately-available methods in core and stdlib. I feel these
are common enough to deserve general inclusion.