floor of log(k)/log(b)

E

ES

Hi,

This expression is giving me some problem: floor(log(k)/log(b)). It
works for many values of k and b (of type double) that I type in, but
when k=8 and b=2, I get floor(log(8)/log(2)) = 2. We know from the
properties of logarithm that this expression should give us a value 3.

I checked log(8)/log(2) = 3. But when I tested it in a simple if
statement if(log(8)/log(2) < 3), it gives me a true statement.

Should I skip this expression altogether and write another function
that behaves like it?

Any help will be appreciated!

--- ES
 
K

Kai-Uwe Bux

ES said:
Hi,

This expression is giving me some problem: floor(log(k)/log(b)). It
works for many values of k and b (of type double) that I type in, but
when k=8 and b=2, I get floor(log(8)/log(2)) = 2. We know from the
properties of logarithm that this expression should give us a value 3.

Note that float, double, and long double do not model the abstract
type "real number". Nothing you learned in school or college about numbers
carries over to floating point numbers.
I checked log(8)/log(2) = 3. But when I tested it in a simple if
statement if(log(8)/log(2) < 3), it gives me a true statement.

Welcome to the miracles of floating point arithmetic.
Should I skip this expression altogether and write another function
that behaves like it?

Yes.

a) It appears you want to find the highest order bit. Google this news
groups for possible solutions. There was similar thread just a few days
ago.

b) Avoid floating point arithmetic unless you know exactly what you are
doing. You may want to read the FAQ sections on floating point arithmetic
and its gotchas.


Best

Kai-Uwe Bux
 
V

Victor Bazarov

ES said:
This expression is giving me some problem: floor(log(k)/log(b)). It
works for many values of k and b (of type double) that I type in, but
when k=8 and b=2, I get floor(log(8)/log(2)) = 2. We know from the
properties of logarithm that this expression should give us a value 3.

I checked log(8)/log(2) = 3.

How did you check? Printed it out? Did you know floats are rounded
during printout?
But when I tested it in a simple if
statement if(log(8)/log(2) < 3), it gives me a true statement.

So, on your systesm (log(8)/log(2)) is not 3. It's close to it, but
is not *precisely* the same.
Should I skip this expression altogether and write another function
that behaves like it?

Depends. What is it you're trying to accomplish with it?

V
 
E

ES

Thanks for the replies. Mathematica has Log[b,k] that gives exact
answers. I wrote a mathlink program that calls this function from C++.

--- ES
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top