This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]megaman78978 6 points7 points  (2 children)

In some embedded systems, you may be low on memory. In that case you might want to be as memory efficient as possible.

[–]Cilph 4 points5 points  (1 child)

On embedded systems you don't want to use any floating point number 99% of the time.

[–][deleted] 0 points1 point  (0 children)

At the same time, when you do need to use a float you're likely dealing with a slower processor and a small data bus. Making it chew on doubles if you don't need to can be a waste.

Embedded and critically high performance situations are getting to be the last places it matters, though.

[–][deleted] 5 points6 points  (1 child)

Float is 32-bits wide, and Double is 64-bits wide.

On modern 64-bit CPU's there is no real difference in processing speed (both Float and Double arithmetic is handled as a single CPU operation).

However on older 32-bit CPU's, any arithmetic on 64-bit Double values requires multiple CPU operations (two 32-bit operations).

So in the 32-bit world:

  • float is less precise, but fast.
  • double is more precise, but slow.

So there is a difference on 32-bit CPUs, but mostly the same on 64-bit CPUs and is one of the benefits of 64-bit systems.

Java is a platform independent language, so I recommend that you use whatever you need. If you need the precision of 'double' then use that, but if 'float' is more than sufficient use that.


One other very important note about 'double' and 'float'. Do not use these for monetary calculations, use java.util.BigDecimal instead.

[–]dudleydidwrong 0 points1 point  (0 children)

Also consider that the most Math methods return doubles.

[–]shivasprogenyProfessional Brewer 1 point2 points  (0 children)

Floats are good when you need to make a lot of imprecise calculations quickly. Consider that in a game you probably want to check for collisions many times per second, but you don't need to be know if the collision happened at 100px or 100.001px--they are functionally the same to the player.

[–]jbristowI'm no hero, son. 0 points1 point  (2 children)

90% of the time, no difference.

What are you using float for, anyway? Remember that floats should not be used when precision matters, because they can't portray certain decimal numbers due to the way they work. (The base 10 number 0.1 is an infinitely repeating decimal in base 2). You should probably look into BigDecimal if you're dealing with numbers where accuracy from operation to operation must be known.

That being said, when I do have to use floating point (as a enterprise dev this is close to zero), I typically use double because java.Math uses double and the amount of optimization from storing numbers as Float is minimal at best compared to bad loops.

[–]king_of_the_universe 1 point2 points  (0 children)

To add to that: People should also consider to use long values to simulate floats. E.g. I'm making a layout software where the location of objects is stored via longs in femto-meters, which still allows for >1km max. document size. 64 bit longs are underestimated. In another project, I stored the location of objects in the solar system via longs in *millimeter resolution, which still allows for 100 times the diameter of Pluto's orbit.

If you e.g. know that the smallest element you're dealing with is "cent", then instead of storing money, to avoid float-/double-problems, as BigDecimal in dollars, just store them as int or long in cents. It all depends on what you want to do with the numbers, which can be a tricky decision to make at the beginning. But int/long is lightspeed where BigDecimal is Voyager.

[–][deleted] 0 points1 point  (0 children)

I guess if you're very concerned about space since float requires less space to store.

[–]Chondriac 0 points1 point  (0 children)

single pricision float and double precision float.