Log base 2 and floats that don't look it

2 things:

  1. Why isn’t there a log base 2 function in the standard Math library?
    Seems pretty essential.

…which leads to:

  1. I found a log2 function in the Extmath lib from the RAA, but it seems
    to return a Float that looks like a Fixnum:

irb(main):004:0> require ‘extmath’
=> true
irb(main):005:0> Extmath.log2 8
=> 3
irb(main):006:0> x = Extmath.log2 8
=> 3
irb(main):007:0> x.class
=> Float #if it were a float, I would expect to see 3.0 not 3 !
irb(main):008:0> x.to_i
=> 2

So Extmath.log2 8 returns 3, but it’s really a Float so it should be 3.0,
how can 3 be a Float? I’m confused, but at least now I know why my code
which was using this log2 function isn’t working.

And to add insult to injury, to_i appears to turn 3 into 2. What gives?

Phil

Hi,

At Fri, 23 Apr 2004 15:54:10 +0900,
Phil Tomson wrote in [ruby-talk:98084]:

So Extmath.log2 8 returns 3, but it’s really a Float so it should be 3.0,
how can 3 be a Float? I’m confused, but at least now I know why my code
which was using this log2 function isn’t working.

It isn’t related with the issue from [ruby-talk:97891]?

···


Nobu Nakada

2 things:

  1. Why isn’t there a log base 2 function in the standard Math library?
    Seems pretty essential.

module Math; def log2(x); log(x)/log(2) end; module_function :log2 end
=> Math
(0…10).map{|x| 2 ** x}.map{|x| Math.log2(x)}
=> [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]

···

On Fri, Apr 23, 2004 at 03:54:10PM +0900, Phil Tomson wrote:


Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

“You, sir, are nothing but a pathetically lame salesdroid!
I fart in your general direction!”
– Randseed on #Linux

Yes, I’ve tested Extmath.log2 8 and Nobu’s patch fixes this problem,
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/97996

···

nobu.nokada@softhome.net wrote:

Hi,

At Fri, 23 Apr 2004 15:54:10 +0900,
Phil Tomson wrote in [ruby-talk:98084]:

So Extmath.log2 8 returns 3, but it’s really a Float so it should be 3.0,
how can 3 be a Float? I’m confused, but at least now I know why my code
which was using this log2 function isn’t working.

It isn’t related with the issue from [ruby-talk:97891]?


Nobu Nakada


Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢
http://photos.yahoo.com/ph/print_splash

In article 200404230706.i3N76eYa010855@sharui.nakada.niregi.kanuma.tochigi.jp,

···

nobu.nokada@softhome.net wrote:

Hi,

At Fri, 23 Apr 2004 15:54:10 +0900,
Phil Tomson wrote in [ruby-talk:98084]:

So Extmath.log2 8 returns 3, but it’s really a Float so it should be 3.0,
how can 3 be a Float? I’m confused, but at least now I know why my code
which was using this log2 function isn’t working.

It isn’t related with the issue from [ruby-talk:97891]?

Thanks. Yes it looks like it probably is related.

Phil

In article 200404230706.i3N76eYa010855@sharui.nakada.niregi.kanuma.tochigi.jp,

···

nobu.nokada@softhome.net wrote:

Hi,

At Fri, 23 Apr 2004 15:54:10 +0900,
Phil Tomson wrote in [ruby-talk:98084]:

So Extmath.log2 8 returns 3, but it’s really a Float so it should be 3.0,
how can 3 be a Float? I’m confused, but at least now I know why my code
which was using this log2 function isn’t working.

It isn’t related with the issue from [ruby-talk:97891]?

Is this fixed in the CVS daily snapshot?

Phil