I am using single precision in my custom function, within which I have an IF statement. The conditional expression for my IF statement tests for equality of two single precision numbers.
I expect that when the difference between two single precision numbers is smaller than what is possible for single precision, these numbers should be "equal" as far as the code is concerned. However, I find that this is not always the case, as the following code illustrates:
function ifSingleTest(power)% ifSingleTest(power)
% Creates delta = single(1/2^power)
% delta should be too small for power >= 24 for single precision.
% However, if "feature accel on", then delta is not too small until power >= 53
% which is the same as for double precision
delta = single(1/2^power) %problem only happens when delta is declared as a single
if single(1+delta) ~= single(1) disp('single(1+delta) ~= single(1)')else disp('delta too small')enddisp([char(13) 'eps(single(1)) = ' sprintf('%1.15e',(eps(single(1))))])
I have found this problem also for the conditional expression of a WHILE loop.
Best Answer