Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts


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?


Share this post

Link to post
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 this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!