gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Is there a better way to cast a float to a integer?

#28235 - Andor - Thu Oct 28, 2004 7:22 am

I'm programming a traditional (final fantasy-esque) roleplaying game, with a battle interface complete with health bars and whatnot. To get the amount of the health bar that should be filled (the health bar is 47 pixels wide), I simply take the currentValue of the given character's health, divide it by the maxValue, and multiply the result by 47.

Code:
u8 GetBarLength(u16 curValue, u16 maxValue) {
   float percentageValue;
   u8 retValue;
   
   percentageValue = 47 * ((float)curValue/(float)maxValue);
   retValue = percentageValue;
   return retValue;
};


This works just fine, except that the compiler spits out a pair of warnings because I cast the float percentageValue to an unsigned byte. Is there a better way to do this - perhaps a way that won't peeve the compiler?

[Images not permitted - Click here to view it]

#28236 - allenu - Thu Oct 28, 2004 7:31 am

Andor wrote:

This works just fine, except that the compiler spits out a pair of warnings because I cast the float percentageValue to an unsigned byte. Is there a better way to do this - perhaps a way that won't peeve the compiler?


That works, but you don't need to use floats at all. Try this instead:

u8 percentageValue = (47 * curValue)/maxValue;

Assigning floats to ints causes the floats to get truncated (not rounded), so that's likely what your compiler was warning you about. Basically, that means if you ever have something like this:

u8 blah = 1.65;

blah will equal 1, and not 2.

#28237 - Andor - Thu Oct 28, 2004 7:42 am

Awesome, thanks for the quick reply. Even though that particular warning wasn't causing any errors, it still feels better to have silent compiles. =)

#28246 - poslundc - Thu Oct 28, 2004 2:02 pm

You might consider not using floats at all and using fixed-point math everywhere instead. Floats will wreak utter havoc with your game's performance, and you don't need them.

Watch out for division, too; it is evil (although sometimes more necessary than floats are).

Check out the beginner's FAQ for more insight.

Dan.

#28250 - isildur - Thu Oct 28, 2004 2:48 pm

What I do instead of using floats or even fixed point, I scale everything by 256. So every unit is 256 instead of 1. This gives me 1/256 precision. When I really need to put it back to a 1 unit size, I just shift by 8. On the gba, it is quite enough precision.

If I need more precision, I use 1/1024 precision instead.

So basically, all my trig code uses this form along with look up tables, so I rarely ever need a division. And I find it a lot simpler to use than fixed point, though it is much the same. But using this method makes it easier to use shifts instead of multiplies.

#28252 - poslundc - Thu Oct 28, 2004 3:24 pm

isildur wrote:
What I do instead of using floats or even fixed point, I scale everything by 256.


Dude, that's fixed point. ;)

Dan.

#28259 - isildur - Thu Oct 28, 2004 4:01 pm

Maybe it is just like fixed point but I don't see it like that. I don't use a fixed point type. My units are simply scaled by 256. It probably comes to the same thing, yes. :)

#28270 - Andor - Thu Oct 28, 2004 7:23 pm

Okay, I'll look into fixed point calculations. I once ran across an axiom that would apply very well to managing the amount of cpu cycles you're taking up: waste not, want not.

Thanks for the suggestions. =)

#28271 - abilyk - Thu Oct 28, 2004 7:39 pm

Cearn's TONC programming guide has a lot of useful information, including a section on fixed-point math.

#28276 - allenu - Thu Oct 28, 2004 8:12 pm

Andor wrote:
Okay, I'll look into fixed point calculations. I once ran across an axiom that would apply very well to managing the amount of cpu cycles you're taking up: waste not, want not.

Thanks for the suggestions. =)


Well, don't start worrying about optimizations until you've got everything working and start noticing that it's too slow.