How to force MATLAB to use single precision ONLY?

37 views (last 30 days)
I am trying to test some algorithms for square root for single precision arithmetic, however MATLAB is doing all of its calculations in double precision arithmetic.
Even if I wrap all of the variables/constants in my equations with the single() command, operations still are done in double precision in some cases (or extra bits are retained through the use of a fused-multiply-add or something of that nature.
So far the only way to correctly compute something like the following all in single preicision arithmetic: c = f/2 + s*s - t/r is NOT to do the following: c = single(single(f)/single(2)) + single(single(s)*single(s)) - single(single(t)/single(r))
INSTEAD, one must do the following: temp1 = single(single(f)/single(2)); temp2 = single(single(s)*single(s)) temp3 = single(single(t)/single(r)); temp = single(single(temp1)+single(temp2)); c = single(single(temp)-single(temp3));
THERE REALLY SHOULD BE AN EASIER WAY TO DO THIS, IT IS SO EASY TO MAKE A MISTAKE AND THIS IS REALLY INCONVENIENT. OTHERWISE MATHWORKS SHOULD INCLUDE THIS FUNCTIONALITY (THE OPTION TO USE SINGLE PRECISION ARITHMETIC ONLY) IN THE NEXT RELEASE OF MATLAB.
Thank You!

Answers (4)

James Tursa
James Tursa on 15 Jul 2013
Good Luck.
I have struggled for years with this difficulty when trying to emulate algorithms that run on a different computer that has no optimization or floating point co-processor available (looking for numerical precision problems in the algorithm). I have basically concluded that there is no easy way to do this. You need to force the storage to happen for every calculation as you are doing and avoid all vectorized operations since there may be a conversion to double or 80-bit in the background that you can't control. This is not just a MATLAB issue btw ... you can run into the same problem with compiled C/C++ or Fortran code on a PC for instance where the compiler will convert your single precision expressions to 80-bit for intermediate operations. I seriously doubt MATLAB will give this functionality in any release (they would have to rewrite all of their library code for some type of "forced-single" class). I don't think you will find an easy answer for this issue, but if you do I would sure like to hear about it!

Philip Borghesani
Philip Borghesani on 15 Jul 2013
You may be able to simplify your coding by subclassing single and using that class.
classdef forcedSingle < single
methods
function out=forcedSingle(in)
out=out@single(in);
end
function out = plus(a, b)
out=forcedSingle(single(a) + single(b));
end
function out = minus(a, b)
out=forcedSingle(single(a) - single(b));
end
function out = times(a, b)
out=forcedSingle(single(a) .* single(b));
end
function out = mtimes(a, b)
out=forcedSingle(single(a) *single(b));
end
function out = rdivide(a, b)
out=forcedSingle(single(a) ./single(b));
end
% Add any method needed
end
end
Be careful I have not tested that this does what you want and any method not overloaded will revert the a standard single operation.
s=forcedSingle(.1);
%use s just like any other single

Jan
Jan on 23 Sep 2013
Edited: Jan on 23 Sep 2013
Matlab 6.5 could not perform calculations with SINGLEs. Therefore I've added a @single folder with all required operations as hardcoded M or MEX files.
Matlab has been developed with a special focus on processing in double precision. E.g. the indexing of arrays is optimized for this type and the support of SINGLEs has been added in newer releases. Several function still do not allow SINGLE input, as fas as I remember FILTER and FILTFILT suffer from this limitation. There is no ISNAN test for the type single in the MEX-API, HISTC converts all the searched values to double before comparing, while the edges are accepted in double format also. These are just some examples, but you can find the preferences of the type double is many details, I assume thousands.
Therefore asking for a switch to perform all operations in the single format is far beyond the idea of Matlab. I assume, a coffee machine toolbox is more likely. Without doubt, both would be useful for some special cases, but I'm afraid that TMW has other goals.

Walter Roberson
Walter Roberson on 23 Sep 2013
You can experiment with the undocumented "feature()" or "system_dependent()" command; see http://www.mathworks.com/matlabcentral/newsreader/view_thread/155369 and http://undocumentedmatlab.com/blog/undocumented-feature-function/

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!