When to use float vs double Java?

336    Asked by AbigailAbraham in Java , Asked on Oct 6, 2022

In every place I've looked, it says that double is superior to float in almost every way. float has been made obsolete by double in Java, so why is it still used?


I program a lot with Libgdx, and they force you to use float (deltaTime, etc.), but it seems to me that double is just easier to work with in terms of storage and memory.


I also read When do you use float and when do you use double, but if float is really only good for numbers with a lot of digits after the decimal point, then why can't we just use one of the many variations of double?


Is there any reason as to why people insist on using floats even though it doesn't really have any advantages anymore? Is it just too much work to change it all?

Answered by Aishwarya Jhadav

The answer to your question - When to use float vs double Java is -


LibGDX is a framework mostly used for game development.

In game development you usually have to do a whole lot of number crunching in real-time and any performance you can get matters. That's why game developers usually use float whenever float precision is good enough.

The size of the FPU registers in the CPU is not the only thing you need to consider in this case. In fact most of the heavy number crunching in game development is done by the GPU, and GPUs are usually optimized for floats, not doubles.

And then there is also:

memory bus bandwidth (how fast you can shovel data between RAM, CPU and GPU)

CPU cache (which makes the previous less necessary)

RAM

VRAM

which are all precious resources of which you get twice as much when you use 32bit float instead of 64bit double.



Your Answer

Interviews

Parent Categories