In the latest update of the Android API the FloatMath
is marked with the following lint-warning:
In older versions of Android, using android.util.FloatMath was recommended for performance reasons when operating on floats. However, on modern hardware doubles are just as fast as float (though they take more memory), and in recent versions of Android, FloatMath is actually slower than using java.lang.Math due to the way the JIT optimizes java.lang.Math. Therefore, you should use Math instead of FloatMath if you are only targeting Froyo and above.
It is also mentioned here that double and float are equal in speed on recent hardware.
I am using some trigonometric math in an application I am currently working on (targeted Froyo and above), but high precision is not needed, so I have been using floats and FloatMath
so far, and there is no need whatsoever to switch to doubles.
However, the "use Math
over FloatMath
"-recommendation does not say which one to use if float is the desired result.
So, in short; which one is preferable?
float foo = FloatMath.sin(bar);
or
float foo = (float) Math.sin(bar);
On a side note, I only have a Froyo-device, so I can't really do any proper benchmarking on my own.
As of API level 22 the FloatMath-class has been deprecated in favor of the regular Math-class.