# 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 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 on other sites
Right. Okay, I'll just use round(). Thanks :)

##### 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 on other sites
Sorry, Iactually posted before I noticed your first post. Okay. I'll do that then :)

##### Share on other sites
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".

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628366
• Total Posts
2982274

• 10
• 9
• 13
• 24
• 11