# Integers for logic, floats for graphics

## Recommended Posts

Hi,

I'm aware that a GPU works with floats (although some newer cards I understand can work with integers). From what I've seen from sample code over the net and on these forums the game logic code also seems to use floats instead of integers, what I don't understand though is why? Is this a case of performance, or what? Is the performance hit from casting an int to a float to perform a matrix operation too great? Similarly, if I were to create a particle effect (for example sparks from a welder), then could these particles be tracked with integers?

Thanks,
NB.

##### Share on other sites
You could track particles with integers.... but what would be the benefit of doing so?

Can you give an example of a situation where you've seen someone use floats, but you thought they could've used integers instead?

Back when CPUs were really bad at floating point ([i]or didn't even support floating point at all[/i]) lots of games used fixed-point math ([i]where you use an integer to represent fractional amounts[/i]). These days, there's no huge performance penalty for float any more, so if a value can be in-between whole numbers ([i]like a position moving naturally through space[/i]), then floats make perfect sense.

On x86, converting between float/int is quick, but on a PowerPC ([i]like in game consoles[/i]) it is actually rediculously slow. So much so that if you're doing mixed logic + math ([i]where the math has to use floats[/i]), it often makes sense to also do the logic using floats.

e.g. on a PPC, instead of:[code]bool conditionA = myFloat >= 12.0f;
bool conditionB = myFloat < 24.0f;
bool conditionC = conditionA && conditionB;
float answer = conditionC ? foo : bar;[/code]You might use:[code]float conditionA = fstep(12.0f, myFloat);//same as (myFloat>=12.0f ? 1.0f : 0.0f)
float conditionB = fstep(myFloat, 24.0f);//same as (myFloat<24.0f ? 1.0f : 0.0f)
float conditionC = conditionA * conditionB;
float answer = fselect(conditionC-1, bar, foo);//same as (conditionC>=1.0f ? foo : bar)[/code]But if the data your working with was integer data (e.g. [font="Courier New"]myInt[/font] as input, and [font="Courier New"]int answer[/font] as output) then you'd write it as you normally would. This is just to avoid the PPC's costly float/int conversions.

##### Share on other sites
Thanks very much for the detailed reply.

## 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

• ### Forum Statistics

• Total Topics
628377
• Total Posts
2982326

• 10
• 9
• 14
• 24
• 11