Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Math.log2 ?

Reply
Thread Tools

Math.log2 ?

 
 
Brian Schroeder
Guest
Posts: n/a
 
      09-02-2004
Hello Group,

I wondered, why there is no log2 function in Math. I expected
Math.log = log2 and
Math.ln = log_e.

And spend some time searching for the bug in my algorithm that used log2.

Now I use
module Math
def log2(n)
Math.log(n) * 1.0 / Math.log(2)
end
end

But this seems quite ugly to me. Is there a reason that log2 is missing,
or have I overlooked something.

Regards,

Brian

--
Brian Schröder
http://www.brian-schroeder.de/
 
Reply With Quote
 
 
 
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      09-02-2004
Hi,

In message "Math.log2 ?"
on Thu, 2 Sep 2004 17:50:19 +0900, Brian Schroeder <(E-Mail Removed)> writes:

|I wondered, why there is no log2 function in Math. I expected
|Math.log = log2 and
|Math.ln = log_e.
|
|And spend some time searching for the bug in my algorithm that used log2.

Mostly because no one told me whether its portable among platforms.
If enough information is provided, I'd love to add it among other math
functions. Besides that, you can check extmath package from RAA now.

matz.


 
Reply With Quote
 
 
 
 
Brian Schroeder
Guest
Posts: n/a
 
      09-02-2004
On Thu, 02 Sep 2004 18:10:17 +0900, Yukihiro Matsumoto wrote:

> Hi,
>
> In message "Math.log2 ?"
> on Thu, 2 Sep 2004 17:50:19 +0900, Brian Schroeder
> <(E-Mail Removed)> writes:
>
> |I wondered, why there is no log2 function in Math. I expected Math.log =
> |log2 and
> |Math.ln = log_e.
> |
> |And spend some time searching for the bug in my algorithm that used log2.
>
> Mostly because no one told me whether its portable among platforms. If
> enough information is provided, I'd love to add it among other math
> functions. Besides that, you can check extmath package from RAA now.
>
> matz.


That is interesting enough. Anybody here would like to explain to me, why
log_2 is harder than log_10 or ln. I just assumed that anything binary
would be nice for computers.

Thanks,

Brian

--
Brian Schröder
http://www.brian-schroeder.de/
 
Reply With Quote
 
Steven Jenkins
Guest
Posts: n/a
 
      09-03-2004
Brian Schroeder wrote:
> That is interesting enough. Anybody here would like to explain to me, why
> log_2 is harder than log_10 or ln. I just assumed that anything binary
> would be nice for computers.


It's neither harder nor easier, it's just not that useful. Base 2
logarithms aren't really needed in most math and engineering. Even in
computer science and information theory, where the base 2 log is an
important analytical concept, it's rarely employed in precision
calculations. Those are not the kind of problems you attack with
numerical methods.

It takes a lot of care and skill to develop a math library; much more
than naively coding power series expansions of transcendental functions.
People tend to put that work where it's really needed.

The good news is that for most purposes in computer science,

log2(x) = ln(x) / ln(2)

is plenty precise. Note that ln(2) is a constant.

Steve


 
Reply With Quote
 
Phil Tomson
Guest
Posts: n/a
 
      09-03-2004
In article <(E-Mail Removed)>,
Steven Jenkins <(E-Mail Removed)> wrote:
>Brian Schroeder wrote:
>> That is interesting enough. Anybody here would like to explain to me, why
>> log_2 is harder than log_10 or ln. I just assumed that anything binary
>> would be nice for computers.

>
>It's neither harder nor easier, it's just not that useful. Base 2
>logarithms aren't really needed in most math and engineering. Even in
>computer science and information theory, where the base 2 log is an
>important analytical concept, it's rarely employed in precision
>calculations. Those are not the kind of problems you attack with
>numerical methods.
>
>It takes a lot of care and skill to develop a math library; much more
>than naively coding power series expansions of transcendental functions.
>People tend to put that work where it's really needed.
>
>The good news is that for most purposes in computer science,
>
>log2(x) = ln(x) / ln(2)
>
>is plenty precise. Note that ln(2) is a constant.



I do occassionally need Log base 2 operations. Mostly I need to figure
out how many bits are needed to represent some range (for example, given a
range of 0 to 7, I need a 3 bit counter) in a hardware representation. I
also ran into a need for log2 when I was doing some Karnaugh map
manipulations for a class project I did last quarter.

You're right though, I don't need any sort of precision for these
applications, integers only:

log2(2) => 1
log2(3) => 2 (it's actually something like 1.5849, apply ceiling op)
log2(4) => 2
log2(5) => 3
log2(6) => 3
log2(7) => 3
log2( => 3
log2(9) => 4
.....

As I recall, since I only needed up log2(32) (5 bits), and since it needed
to be as fast as possible, I just defined my own method that uses a case
statement to 'lookup' the desired value.


Phil
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments