Sign in to follow this  
Erondial

Log Question

Recommended Posts

Okay, I'm trying to use the log function... but the answer it's returning is just plain wrong.
if (structure->w >= structure->h)
	structure->qu = (short unsigned int)(log(structure->w)/log(2));
else
	structure->qu = (short unsigned int)(log(structure->h)/log(2));
Okay. Structure->w == 64, and structure->h == 64. However, struture->qu is set to 5. It should be set to 6. I've verified the variables before and after the functions, they're 64 throughout. I checked the result with a calculator, and on the calculator, I'm getting 6, yet the computer still smacks a 5 in there. Any idea as to why this might happening?

Share this post


Link to post
Share on other sites
When you convert a number to an integer in cpp, it rounds it down.
You might be getting floating point rounding errors.
So if
(log(structure->w)/log(2)) = 5.999999,
(short int) (log(structure->w)/log(2)) will equal 5

Share this post


Link to post
Share on other sites
Er, round() doesn't sound like a good idea if it uses the standard 0.5 cutoff. You want all values to be "bumped up" to the next smallest integer. So 5.1 needs to bumped up to 6 just as much as 5.9. But if you have values like 5.0 or 6.0 then they stay the way they are.

Share this post


Link to post
Share on other sites
I would advise something like:
ciel(x-0.0000001)

because something that should be exactly 5.0 could turn out to be 5.00001 very easily. Unless you don't care about the occassional "+1".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this