Should I use double or float?

What are the advantages and disadvantages of using one rather than the other in C ++?

+68
c ++ floating-point types double-precision
Jul 02 '09 at 13:53
source share
11 answers

If you want to know the true answer, you should read What Every Computer Scientist Should Know About Floating-Point Arithmetic .

In short, although double allows higher precision in its presentation, it will produce large errors for certain calculations. The "right" choice: use as much as you need, but no more and choose the right algorithm .

In any case, many compilers extend the mathematics of floating point in the "non-line" mode (that is, they use a wider type of floating point available at the hardware level, for example, 80-bit and 128-bit), this should also be taken into account. In practice, you can see any difference in speed . In any case, they are natives on equipment.

+88
Jul 02 '09 at 14:01
source share

This question cannot be answered as there is no context for the question. Here are some things that can influence your choice:

  • The implementation of the float compiler doubles and doubles. The C ++ standard states:

    There are three types of floating point: float, double and long double. A double type provides at least the same precision as a float, and a double double double provides at least the same precision as a double.

    So, all three can be the same size in memory.

  • The presence of FPU. Not all processors have FPUs, and sometimes floating point types are emulated, and sometimes floating point types are simply not supported.

  • FPU architecture. The IAP FPU has an internal internal memory of 80 bits - 32 bits, and 64 bits - 80 bits each at load and reduced in stock. There is also a SIMD that can run four 32-bit floats or two 64-bit floats in parallel. The use of SIMD is not defined in the standard, so a more complex analysis is required for the compiler to determine whether it is possible to use SIMD or use special functions (libraries or internal functions). The result of the 80-bit internal format is that you can get slightly different results depending on how often the data is stored in RAM (thus, losing accuracy). For this reason, compilers do not optimize floating point code particularly well.

  • The width of the memory. If a double requires more memory than a float, it takes longer to read the data. This is a naive answer. On modern IA32, it all depends on where the data comes from. If it is in the L1 cache, the load is negligible, provided that the data comes from one cache line. If it spans more than one cache line, there is little overhead. If it is from L2, it takes some more time, if it is in RAM, then it is even longer, and finally, if it is a huge time on disk. Thus, choosing float or double is less important than the way you use data. If you want to do a small calculation on a set of sequential data, a small data type is preferred. Performing a large number of calculations on a small data set will allow you to use larger data types with any significant effect. If you access the data in a very random way, the choice of data size is unimportant - the data is loaded into page / cache lines. Therefore, even if you only need a byte from RAM, you can get 32 โ€‹โ€‹bytes (this very much depends on the system architecture). In addition to all this, the CPU / FPU can be superscalar (aka pipelined). Thus, even if the load may take several cycles, the CPU / FPU may be busy doing something else (for example, multiplication), which hides the load time with a sufficient degree of time.

  • The standard does not apply any specific format for floating point values.

If you have a specification, then this will help you choose the best option. Otherwise, to understand what to use.

+26
Jul 02 '09 at 14:43
source share

If you have any specific reason for this, use double.

Perhaps surprisingly, this is a double and not a float, which is a โ€œnormalโ€ floating point type in C (and C ++). Standard math functions, such as sin and log , take doubling as arguments and return doubling. A regular floating-point literal, as when writing 3.14 in your program, is of type double. Do not swim.

On typical modern computers, doubling can be as fast as floats or even faster, so performance is usually not a factor to consider even for large calculations. (And it should be a lot of computing, or performance should not even enter your mind. My new i7 desktop can do six billion doubles in one second.)

+26
Sep 25 '09 at 8:12
source share

Double is more accurate, but encoded in 8 bytes. float is only 4 bytes, therefore less space and less accuracy.

You have to be very careful if you have double and floating in the application. Because of this, I had a mistake. One part of the code used float, while the rest of the code used double. Copying double to float and then float to double can lead to precision errors, which can have a big impact. In my case, it was a chemical factory ... I hope that it had no dramatic consequences :)

I think that due to this kind of error, the Ariane 6 rocket exploded several years ago !!!

Think about the type that will be used for the variable

+11
Jul 02 '09 at 14:21
source share

I personally repeat all the time until I see some bottlenecks. Then I look at moving to a float or optimizing some other part

+6
Jul 02 '09 at 13:57
source share

It depends on how the compiler implements double. This is legal for double and float for the same type (and it is on some systems).

If we say that they are really different, the main problem is accuracy. The twin has much higher accuracy due to size difference. If the numbers you use usually exceed the float value, then use double.

Several other people mentioned results. That would be exactly on my list of considerations. Correctness should be your No. 1 consideration.

+4
Jul 02 '09 at 13:58
source share

Use any precision necessary to achieve the appropriate results . If you find that your code is not working as well as you would like (did you use profiling correctly?) Take a look at:

+3
Jul 02 '09 at 14:04
source share

I think that regardless of the differences (which, as everyone notes, floats take up less space and tend to be faster) ... does anyone ever run into performance issues using double? I say I use double ... and if later you decide "wow, this is really slow" ... find a performance bottleneck (which is probably not the fact that you used double). THEN, if it is still too slow for you, look where you can sacrifice some precision and use a float.

+2
Jul 02 '09 at 14:01
source share

It is highly processor dependent, the most obvious trade-offs between accuracy and memory. With RAM, memory is not a big problem, so it is usually better to use double s.

In terms of performance, this is highly processor dependent. float will usually have better performance than double on a 32-bit machine. At 64 bits, double sometimes faster since this is (usually) their own size. However, it will matter far more than your choice of data types, whether you can use SIMD instructions on your processor.

+1
Jul 02 '09 at 14:14
source share

double has higher precision, while floats take up less memory and faster. In general, you should use a float if you have no case where it is not accurate enough.

0
Jul 02 '09 at 13:55
source share

The main difference between float and double is accuracy. Wikipedia has more information about Single precision (float) and Double precision .

0
Jul 02 '09 at 13:58
source share



All Articles