# Log Question

## Recommended Posts

Erondial    172
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
meisawesome    186
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

Zipster    2365

##### Share on other sites
Erondial    172
Right. Okay, I'll just use round(). Thanks :)

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

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