MATLAB: Double vs Single Precision, Matlab in 2019

MATLABsingle precision

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

Best Answer

This topic is worth discussing.
X = zeros(10,10, 'single');
The construction: X = single(zeros(10,10)); is four times slower!!
Of course it is! Why would you not expect that? The former just fills the assigned memory locations with the same zero element. The latter fills it with double precision zeros, then needs to move the entire array to a new location, converting each element to a single on the fly.
"In addition, single precision uses half the memory, and is generally twice as fast."
Had you stated that it is SOMETIMES twice as fast, you would be correct. Newer releases have improved in this respect, although users wth older releases may find that single is no faster than double computations.
The problem is that single precision can be dangerous. You may not always know when you are near the edge of a numerical cliff. Using doubles keeps you much farther away from that edge. Yes, good numerical analysis, good numerical methods is a good thing. But if you use singles just to save some CPU time and some memory, then you are forgetting a major thing - CPU time is cheap, as is memory. So if you are using singles just to be frugal, then you are making a mistake. Use single precision when you absolutely need to do so, and only when you have the numerical skills to know that you can safely afford the lower precision.
Effectively, if you use single precision for no valid reason than pure frugality, then you are pre-optimizing your code, often a bad thing.
"Matlab defaults to double precision, but single precision is sufficient for many..."
A better way to say that is:
Matlab defaults to double precision, but you can sometimes survive the use of single precision. As the precision gets smaller, the risks grow greater.