Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what situations is it better to use a float over a double in Java? [closed]

More specifically, how extreme is the performance gain when using a float instead of a double in Java?

like image 693
Julian Avatar asked Jan 30 '14 00:01

Julian


1 Answers

There are two conditions that should be met to use float instead of double:

  1. There is a significant, useful performance gain.
  2. Float is precise enough for the calculation in question.

Evaluating the first condition is relatively simple and easy. Measure the job using double. Measure again, with the same inputs and other conditions, using float. Is the float version significantly faster? Generally, the main benefit comes from more efficient use of caches, memory, and data transfer bandwidth.

The difficult part is evaluating the second condition. Getting insufficiently precise answers, or even wrong answers, is useless. For many calculations, relatively simple analysis can show double is precise enough. Showing the same for float is often much harder.

like image 94
Patricia Shanahan Avatar answered Oct 11 '22 15:10

Patricia Shanahan