Skip to main content
gamedev.net
🔒 Locked

Log Question

Started by Erondial Apr 24, 2006 at 5:19 PM 5 replies 650+ views
Original Post
Erondial
Erondial
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?
meisawesome
meisawesome
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
Zipster
You'll want to make use of ceil/ceilf on your answer.
Erondial
Erondial
Right. Okay, I'll just use round(). Thanks :)
Zipster
Zipster
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.
Erondial
Erondial
Sorry, Iactually posted before I noticed your first post. Okay. I'll do that then :)
NotAYakk
NotAYakk
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".

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.