Skip to content Skip to sidebar Skip to footer

Float Or Double?

Which is faster, double or float, when preforming arithimic (+-*/%), and is it worth just using float for memory reasons? Precision is not an issue much of an issue. Feel free to c

Solution 1:

The processing speed on both types should approximately be the same in CPUs nowadays. "use whichever precision is required for acceptable results." Related questions have been asked a couple of times here on SO, here is one.

Edit:

In speed terms, there's no difference between float and double on the more modern hardware.

Please check out this article from developer.android.com.

Solution 2:

Double rather than Float was advised by ADT v21 lint message due to the JIT (Just In Time) optimizations in Dalvik from Froyo onwards (API 8 and later).

I was using FloatMath.sin and it suggested Math.sin instead with the following under "explain issue" context menu. It reads to me like a general message relating to double vs float and not just trig related.

"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."

Hope this helps.

Solution 3:

I wouldn't advise either for fast operations but I would believe that a operations on floats would be faster as they are 32 bit vs 64 bit in doubles.

Solution 4:

http://developer.android.com/training/articles/perf-tips.html#AvoidFloat

Avoid Using Floating-Point

As a rule of thumb, floating-point is about 2x slower than integer on Android-powered devices.

In speed terms, there's no difference between float and double on the more modern hardware. Space-wise, double is 2x larger. As with desktop machines, assuming space isn't an issue, you should prefer double to float.

Also, even for integers, some processors have hardware multiply but lack hardware divide. In such cases, integer division and modulus operations are performed in software—something to think about if you're designing a hash table or doing lots of math.

Solution 5:

a float is 32 bits or 4 bytes

a double is 64 bits or 8 bytes

so yeah, floats are half the size according to the sun java certification book.

Post a Comment for "Float Or Double?"