# Integers for logic, floats for graphics

This topic is 2593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 (or didn't even support floating point at all) lots of games used fixed-point math (where you use an integer to represent fractional amounts). These days, there's no huge performance penalty for float any more, so if a value can be in-between whole numbers (like a position moving naturally through space), then floats make perfect sense.

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

e.g. on a PPC, instead of:bool conditionA = myFloat >= 12.0f; bool conditionB = myFloat < 24.0f; bool conditionC = conditionA && conditionB; float answer = conditionC ? foo : bar;You might use: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)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.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999983
×