Single vs Double datatypes

Are there situations where it would be wiser to use one data type instead of a double? From my search, the drawback of the double is that it requires more space, which is not a problem for most applications. In this case, should all floating point numbers be doubled?

Some background information: I am working with an application that deals with a lot of data on coordinates and chemicals. Some customers have noticed that when importing data tables, some values โ€‹โ€‹are rounded with precision to the accuracy of one.

+6
double floating-point types
source share
6 answers

On most desktop applications, yes.

Although if you have a huge array of them, half the size can be large enough to be useful if you don't need precision.

Especially considering that almost all desktop computers have double precision floating-point arithmetic made in hardware.

+2
source share

From this .net article

Data width

The most efficient data types are those that use the native data width of the runtime platform. On the current platform, the data width is 32 bits, both for the computer and for the operating system.

Consequently, Integer is currently the most efficient data type in Visual Basic .NET. The next best ones are Long, Short, and Byte, in this order of effectiveness. You can improve Short and Byte performance by disabling the integer overflow check, such as setting the RemoveIntegerChecks property, but this carries the risk of incorrect calculations due to undetected overflows. You cannot enable or disable this check at runtime; You can only set its value for the next build of your application.

If you need fractional values, Double is the best choice, because the floating-point processors of the current platform perform all operations in double precision. The next best ones are Single and Decimal, in order of effectiveness.

+4
source share

As Mark notes in his comment, space can be a problem for memory-limited systems. You can also index or sort the list, and why do you need to double it if you can store your values โ€‹โ€‹in single player games?

+2
source share

On some hardware, arithmetic involving double values โ€‹โ€‹may take longer than single values, but the latest FPUs have one proprietary data type (e.g. 80-bit extended floating point values โ€‹โ€‹for x86) that will be used internally for calculations regardless what type of data in memory you are using. So, to say that "FPU computing will be faster with single precision" is usually not the reason for using single-precision on most modern hardware today.

However, in addition to the โ€œuses less memoryโ€ reasons described in other answers, there is a very practical reason when it comes to vector SIMD instructions such as SSE and AltiVec. Single precision is twice as fast as double precision, because the instructions work on fixed-size vectors, and you can write twice as many single-precision values โ€‹โ€‹into one vector, and the processing time usually remains the same.

For example, with a 128-bit vector unit capable of processing vector multiplications in 2 clock cycles, you can get the bandwidth from two single precision multiplications per cycle compared to 1 double precision, since you can 4 singles in a vector, against two two-local ones.

A similar effect occurs with memory bandwidth and is not specific to vector processing - if you have large arrays of doubles, they not only take up twice as much space, but can take up twice as much time to process when your algorithm is limited with bandwidth (which is becoming more likely given the increase in size and decrease in latency of vector processing units).

+2
source share

Double places take up more space, but additional accuracy may or may not be needed. I programmed a lot in the scientific world, where floating point arithmetic is very common and found that often you can do the calculations with double or higher precision, but save the results as singles without a bad effect.

Keep in mind that once numbers are absorbed into the FPU, they will still expand to very high precision. It would be better to try everything that you do in both cases and see if the results are comparable.

Unfortunately, computing is still an experimental science.

+1
source share

If you are coding OpenGL, then it is ok to use GLSingle (e.g. single), not GLDouble. In almost all cases, single precision is more than enough for most graphic applications and should be faster - although I admit that I am not sure about this on the latest generations of GPUs.

My favorite quote is that only accuracy was enough to go to the moon and back, so in practice it is unusual to cause a real problem. However, in most cases, I would choose the double option for now, since storage is cheap, and less likely to be some kind of odd binary system for decimal problems.

0
source share

All Articles