Math.log2 ?

  • Thread starter Brian Schroeder
  • Start date
B

Brian Schroeder

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
 
Y

Yukihiro Matsumoto

Hi,

In message "Math.log2 ?"

|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.
 
B

Brian Schroeder

Hi,

In message "Math.log2 ?"
on Thu, 2 Sep 2004 17:50:19 +0900, Brian Schroeder

|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
 
S

Steven Jenkins

Brian said:
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
 
P

Phil Tomson

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(8) => 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top