Double vs Single Precision, Matlab in 2019
Show older comments
Matlab defaults to double precision, but single precision is sufficient for many computational problems. In addition, single precision uses half the memory, and is generally twice as fast.
To convert from double to single precision is easy, in that all that is needed is to define the starting variables as single, and then all subsequent variables will default to the variable in each calculation with the lowest precision.
The best approach to define type single is:
X = zeros(10,10, 'single');
The construction: X = single(zeros(10,10)); is four times slower!!
Note that there is no difference between these two constructions for type double given that double precision is the default.
PROBLEMS
I ran into two problems that dimmed my enthusiasm for single precision.
1) Some Matlab functions will only work with double precision. The interpolation functions are the main examples that I have come across. (It would be great to have some documentation, either informal or formal, listing those Matlab functions that have type limitations. I have yet to find this information on the web.) To use the interpolation functions, you have to convert the input arguments to double, and then convert the output arguments back to single, which is cumbersome, and perhaps troublesome as well, as noted below.
2) A second problem is that the transition back and forth between single and double can cause errors in the limits for the interpolation. I ran across an error where the interpolation started to return nans after conversion to single precision. This problem would not have occured if the interpolation functions were able to work with single precision. That said, it might be possible to set the extrapolation option to "nearest" in order to navigate around the small round-off errors associated with the transition between single and double precision. Of course, this option could only be trusted when the algorithm was known to be fully functional.
For now, I have decided to stick with double precision.
I found very little discussion of this issue on the web. Surely there are others out there with experiences and recommendations about single precision.
Best,
Mark
Accepted Answer
More Answers (4)
Krishna Bindumadhavan
on 14 Sep 2019
1 vote
Although this topic is about single precision, another floating point type with reduced precision that we are actively working on supporting with MATLAB is the half precision data type available with the fixed point designer toolbox:https://www.mathworks.com/help/fixedpoint/ref/half.html. With GPU Coder, you can deploy trained neural networks with half precision optimizations from 19a onwards.
Although half is usually not suitable for general purpose scientific computing, several applications like deep learning (training + inference) and image processing have proven benefits for using half precision to reduce memory bandwidth and computation time if the application can tolerate the reduced precision (as usually the case with deep learning).
On recent GPU's from NVIDIA like the Turing and Volta series, there is dedicated support for half precision in the hardware via tensor cores, which can accelerate computations like matrix multiply up to 8x.
This area is under active development and we expect to improve and expand support for half in various products in coming releases.
Walter Roberson
on 17 Feb 2019
0 votes
>> x = linspace(single(0),single(pi),20);
>> y = single(rand(1,20));
>> z = interp1(x, y, linspace(single(0),single(1),20))
z =
1×20 single row vector
Columns 1 through 9
0.7323988 0.5099056 0.2874124 0.06491931 0.2007067 0.3955918 0.5904768 0.6504743 0.6571828
Columns 10 through 18
0.6638914 0.6852249 0.7173581 0.7494914 0.7815178 0.8134047 0.8452916 0.8761108 0.9043419
Columns 19 through 20
0.9325731 0.9475382
That looks like it supports single to me.
x— Sample points
vector
Data Types: single| double| duration| datetime
That looks like it supports single to me.
Mark Brandon
on 17 Feb 2019
0 votes
Mark Brandon
on 12 Aug 2019
Edited: Mark Brandon
on 12 Aug 2019
0 votes
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!