- Home /
Does it make any difference whether position is an integer or a float?
Does it matter whether the position, rotation, scale or anything else is set to 1 or 0.23452345...? I'm wondering this since the number '1' uses less memory to store, and thus it requires less time to get the information when the scripts are run. I usually try to restrict the use of floats, or at least round the number "0.259999" up to "0.26". Does this actually make any difference in any way? Or am I just wasting my time doing it?
Floats in C# are 32 bits, therefore both the number "0.23452345" uses 32 bits of memory, and the number "1" also uses 32 bits of memory.
Ints in C# are also 32 bits, so the number "0" as an Int, is still 32 bits.
Perfor$$anonymous$$g operations on Int's is generally faster than floats, but then it only works if you are actually operating on int types, just rounding a float to a whole number and storing the result as a float won't do anything.
Short answer. Probably just wasting your time.
Position, rotation, & scale all use floats so I can't see what you might gain from this.
If your project is complete and working and you need to optimize it then sure you could go back and look to see if some numbers could be rounded out. But remember that the process of rounding is extra code.
Answer by space928 · Oct 12, 2017 at 07:39 PM
Well, floating point numbers have 32 bit precision, meaning in memory they take up 32 bits (+ a bit of overhead). Rounding the number just means that the rest of digits are 0s (so rounding 0.259999 results in 0.260000...). Unless some very clever memory management is used, then it will take up the same amount of space. But either way unless you need to store like millions of floats (which would still only be in the megabytes/mebibytes) so worrying about this isn't always important. The performance difference difference between floats and ints on the CPU (not the GPU which happens to be the other way round) is noticeable.
Alright, so to optimize the scripts and the game I should stick to ints, correct?
Yeah, but modern CPUs are quite fast with both floats and ints so the difference is only small. When trying to optimise your game, try and focus on things like redundant iteration, which will take up much more time than floats versus ints.
No. You seem to have a very strange view on how a number is actually stored in memory. The actual value of the number doesn't matter at all. Only the used variable format dictates how much memory is needed. A normal "int" is a 32 bit integer value. It always requires 4 bytes of memory no matter if you store the number "0", "42" or "2147483647" it will always use those 4 bytes.
Likewise a single precision float also uses 32 bits and also requires 4 bytes at all times, no matter what value you store in it. It can be "0", "42.12345", "1.984565E45", "2.5E-32" or "+inf"
The next thing is certain floating point values can't be rounded to a better representation. For example the value 0.26 can't be represented as a binary number. The closest value below 26 is "0.2599999904632568359375" and the next larger number would be "0.2600000202655792236328125". Typically the first value will show as "0.26" as it's rounded after the 8th or 9th place.
If you're interested in how the IEEE 754 format works, here's an interactive converter and here's the wikipedia article
Choosing which datatype you want to use is mainly a matter of what you're going to do with it. For example representing a position with an integer wouldn't make much sense unless you don't want smooth movement but only jump from one grid position to the next.
However under the hood Unity uses floats for almost everything. So using a different data type in your own structures makes no sense as in the end it has to be converted into a float anyways. Also your GPU almost exclusively works on 32bit floating point and fix point values.
@space928: What kind of "overhead" do you mean? 32 bits are 32bits nothing more, nothing less. It uses 23 bits as mantissa, 8 bits as exponent and 1 sign bit which makes up 32 bits.
Answer by DaDonik · Oct 13, 2017 at 06:44 AM
Float and int use the same amount of memory (32bit).
If you are working with transforms, the position, rotation and scale already are floats. That means that you need to constantly cast between int and float (happens implicitly), which is guaranteed to be slower than using floats only.
I like the fact that you think about performance, but this won't save you even a single CPU cycle.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Moving object with transform.position ignore other objects even if they collided 1 Answer
Need enemy to follow only active character 0 Answers
Drawing a (debug) line between anchored UI element and mouse? 0 Answers
Using Dictionaries to store and retrieve GameObjects? 1 Answer