Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Frequently in my programming experience I need to make a decision whether I should use float or double for my real numbers. Sometimes I go for float, sometimes I go for double, but really this feels more subjective. If I would be confronted to defend my decision, I would probably not give sound reasons.

When do you use float and when do you use double? Do you always use double, only when memory constraints are present you go for float? Or you use always float unless the precision requirement requires you to use double? Are there some substantial differences regarding computational complexity of basic arithemtics between float and double? What are the pros and cons of using float or double? And have you even used long double?

share|improve this question
9  
In many cases you want to use neither, but rather a decimal floating or fixedpoint type. Binary floating point types can't represent most decimals exactly. –  CodesInChaos Feb 28 '13 at 11:20
    
Related to What causes floating point rounding errors?. @CodesInChaos my answer there suggests resources to help you make that determination, there is no one-size-fits-all solution. –  Mark Booth Feb 28 '13 at 13:26
    
Very good answer found at: Stack Overflow –  Haris Feb 28 '13 at 13:36
    
For decimals I would use neither. I would use an integer and store the value multiplies by 100. –  Loki Astari Feb 28 '13 at 15:35
4  
What exactly do you mean by "decimals". If you need to represent values like 0.01 exactly (say, for money), then (binary) floating-point is not the answer. If you merely means non-integer numbers, then floating-point is likely ok -- but then "decimals" is not the best word to describe what you need. –  Keith Thompson Feb 28 '13 at 16:22
show 2 more comments

4 Answers

up vote 50 down vote accepted

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. exp, sin, etc.).

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.

In summary, float and long double should be reserved for use by the specialists, with double for "every-day" use.

share|improve this answer
4  
I would probably not consider float for a few thousand values unless there were a performance problem related to floating point caching and data transfer. There is usually a substantial cost to doing the analysis to show that float is precise enough. –  Patricia Shanahan Feb 28 '13 at 15:35
2  
As an addendum, if you need compatibility with other systems, it can be advantageous to use the same data types. –  zzzzBov Feb 28 '13 at 16:30
add comment

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:

  1. You are storing large arrays of numbers and need to reduce your program's memory consumption.
  2. You are targeting a system that doesn't natively support double-precision floating point. Until recently, many graphics cards only supported single precision floating points. I'm sure there are plenty of low-power and embedded processors that have limited floating point support too.
  3. You are targeting hardware where single-precision is faster than double-precision, and your application makes heavy use of floating point arithmetic. On modern Intel CPUs I believe all floating point calculations are done in double precision, so you don't gain anything here.
  4. You are doing low-level optimization, for example using special CPU instructions that operate on multiple numbers at a time.

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.

share|improve this answer
add comment

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 float numbers.

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.

E.g. 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 float[].

share|improve this answer
add comment

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.

share|improve this answer
17  
Using floating point for serious money calculations is probably a mistake. –  Bart van Ingen Schenau Feb 28 '13 at 10:53
10  
float is exactly the wrong type for money. You need to be using the highest precision possible. –  ChrisF Feb 28 '13 at 10:56
6  
@BartvanIngenSchenau Floating point for money is usually okay, binary floating point is not. For example .net's Decimal is a floating point type and it's typically a good choice for money calculations. –  CodesInChaos Feb 28 '13 at 11:21
1  
However, based on those facts, one can conclude that it's not floating point in the same sense as we normally use those words -it's fixed point, except that the scaling factor is a member of the type instead of being defined by the type. –  GalacticCowboy Feb 28 '13 at 17:27
5  
@ChrisF You don't need "high precision" for money, you need exact values. –  Sean McSomething Feb 28 '13 at 19:37
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.