Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Android, should I use the float datatype more often for my game, or just cast to float when drawing?

Tags:

java

android

Android uses the RectF structure for drawing bitmaps. I am working on my game structure, so for example, a Sprite will have and x/y coordinate, width, height, speed, and so on. This means every time in my render loop I have to cast those integers to floats when figuring out the source/target RectF's to use... alternatively, I can be far more universal and use floats everywhere so that when it comes time to simulate the physics and render, all of the types are already of the same type... even if it is unnecessary for what the property is (I don't need a float for "x position", but will have to cast it when rendering if not).

if floats generally are 2-10x's more ineffient (per http://developer.android.com/guide/practices/performance.html), what is the proper course to take?

TLDL: Should I cast int's to float on render, or just have all of the contributing variables be floats to begin with, even if floats are inefficient? How inefficient is a typecast?

like image 446
user11398 Avatar asked Sep 12 '12 16:09

user11398


People also ask

When should I use float?

float should only be used if you need to operate on a lot of floating-point numbers (think in the order of thousands or more) and analysis of the algorithm has shown that the reduced range and accuracy don't pose a problem.

Is it better to use float or double?

double is mostly used for calculations in programming to eliminate errors when decimal values are being rounded off. Although float can still be used, it should only be in cases when we're dealing with small decimal values. To be on the safe side, you should always use double .

Is there any reason to use float over double?

Float and double Double is more precise than float and can store 64 bits, double of the number of bits float can store. Double is more precise and for storing large numbers, we prefer double over float. For example, to store the annual salary of the CEO of a company, double will be a more accurate choice.

Are floats faster than doubles?

Floats are faster than doubles when you don't need double's precision and you are memory-bandwidth bound and your hardware doesn't carry a penalty on floats. They conserve memory-bandwidth because they occupy half the space per number. There are also platforms that can process more floats than doubles in parallel.


1 Answers

The best way to do this is to do your calculations with the highest degree of precision required to produce the expected results within the specified tolerance. That means if you need to use doubles to do calculations and get the expected results with consistency, then use them. Figure out where less precision is acceptable, and only do the operations that require it with floats. Remember, your world doesn't have to approximate earth gravity for falling objects, you could simplify things and make the gravity 10 instead of 9.81, make pixels correspond to even units, etc. It'll also help if you define your constants in the same units and avoid doing unit conversion to do math (as this results in extra ops), it's better to add more final constants that have something like gravity in cm/s, m/s and km/h than it is to only have one and to convert it a thousand times. Oh and the cost of casting int to float isn't very high compared multiplying 2 floats, so think about that.

I'll also note that FPUs are becoming more and more common in modern android phones, and so the issue of using floating point math is becoming a little less important (although not entirely).

The other thing I want to note is Double/Float/Integer vs. double/float/int. The former require new objects to be created to use and shouldn't be used for math (whenever possible), whereas the latter are primitives and do not result in new object creation to use, they're less costly to create.

like image 159
hsanders Avatar answered Oct 13 '22 13:10

hsanders