Usually, I use the
float type when I don't need much precision — for example, for money — which is wrong, but is what I'm used to wrongly do.
On the other hand, I use
double when I need more precision, for example for complex mathematical algorithms.
The C99 standard says this:
There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double.
I never really used
long double, but I don't use C/C++ so much. Usually I use dynamically typed languages like Python, where you don't have to care about the types.
For further information about Double vs Float, see this question at SO.
The default choice for a floating-point type should be
double. This is also the type that you get with floating-point literals without a suffix or (in C) standard functions that operate on floating point numbers (e.g.
float should only be used if you need to operate on a lot of floating-point numbers (think in the order of thousands or more) and analysis of the algorithm has shown that the reduced range and accuracy don't pose a problem.
long double can be used if you need more range or accuracy than
double, and if it provides this on your target platform.
long double should be reserved for use by the specialists, with
double for "every-day" use.
double for all your calculations and temp variables. Use
float when you need to maintain an array of numbers -
float (if precision is sufficient), and you are dealing with over tens of thousands of
Many/most math functions or operators convert/return
double, and you don't want to cast the numbers back to
float for any intermediate steps.
If you have an input of 100,000 numbers from a file or a stream and need to sort them, put the numbers in a
There is rarely cause to use float instead of double in code targeting modern computers. The extra precision reduces (but does not eliminate) the chance of rounding errors or other imprecision causing problems.
The main reasons I can think of to use float are:
So, basically, double is the way to go unless you have hardware limitations or unless analysis has shown that storing double precision numbers is contributing significantly to memory usage.
For real world problems the sampling threshold of your data is important when answering this question. Similarly, the noise floor is also important. If either is exceeded by your data type selection, no benefit will come from increasing precision.
Most real world samplers are limited to 24 bit DAC s. Suggesting that 32 bits of precision on real world calculations should be adequate where the significand is 24 bits of precision.
Double precision comes at the cost of 2x memory. Therefore limiting the use of doubles over floats could drastically cut the memory footprint/bandwidth of running applications.
Some platforms (ARM Cortex-M2, Cortex-M4 etc) don't support double (It can always be checked in the reference manual to your processor. If there is no compilation warnings or errors, it does not mean that code is optimal. double can be emulated.). That is why you may need to stick to int or float.
If that is not the case, I would use double.
You can check the famous article by D. Goldberg ("What Every Computer Scientist Should Know About Floating-Point Arithmetic"). You should think twice before using floating-point arithmetic. There is a pretty big chance they are not needed at all in your particular situation.
The choice of what variable to use between float and double depends on the accuracy of the data required. If an answer is required to have negligible difference from the actual answer, number of decimal places required will be many thus will dictate that double to be in use.Float will chop off some decimal places part thus reducing the accuracy.