# MatLab single precision epsilon versus double precision epsilon

29 views (last 30 days)
Aarav Shah on 14 Jan 2022
Commented: John D'Errico on 14 Jan 2022
when I solve for the epsilon of one, using a basic method. I get the following values for single and double precision.
double precision: 2.2204e-16
single precision: 1.1921e-07
why is that? I am 99% sure matlab's machine epsilon is correct for double precision. can somebody inform me if I am correct or incorrect? I will attach code later on.
Stephen23 on 14 Jan 2022
eps(double(1))
ans = 2.2204e-16
eps(single(1))
ans = single 1.1921e-07

John D'Errico on 14 Jan 2022
Edited: John D'Errico on 14 Jan 2022
By the way. I am 100% sure that MATLAB's machine epsilon is correct for both single and double. :)
One subtle issue lies in the code you wrote. You show this in a comment:
epsilon = 1.0;
while single((1.0 + 0.5*epsilon)) ~= single(1.0)
epsilon = 0.5*epsilon;
end
format long
disp(epsilon)
1.192092895507812e-07
Note that the above is poor code, since it defines epsilon as a DOUBLE. And every computation that follows that line maintains epsilon as a double. When you write this
epsilon = 1.0;
That line creates a DOUBLE PRECISION variable. If you really wish to investigate single precision behavior, then you need to use single precision variables.
Anyway, what is eps for a single?
format long g
eps('single')
ans = single
1.192093e-07
eps('single')*2^23
ans = single
1
which is indeed 2^-23.
In fact, it is the same as the result you got, despite the poor choice of trying to compute machine epsilon for a single, by the use of fully double precision computations! The only place where you ever used singles in your code was in the test. And that is a really dangerous thing to do. Had you done this instead:
epsilon = single(1);
while (1 + 0.5*epsilon) ~= 1
epsilon = 0.5*epsilon;
end
format long
disp(epsilon)
1.1920929e-07
Now all computations were done in single precision, since operations like .5*epsilon will still produce a single result when epsilon is a single. See that I never needed to convert anything to a single. And after having performed that loop, what class is epsilon in my version of your code?
class(epsilon)
ans = 'single'
Anyway, all of these results have indeed produced effectively the same result in the end. Perhaps your issue lies in the last digits reported? You need to be very careful there, as disp is NOT reporting the exact number as stored in the variable epsilon. It rounds off the result.
fprintf('%0.55f',epsilon)
0.0000001192092895507812500000000000000000000000000000000
Epsilon here is the value produced by the SINGLE precision computations.
fprintf('%0.55f',2^-23)
0.0000001192092895507812500000000000000000000000000000000
fprintf('%0.55f',eps('single'))
0.0000001192092895507812500000000000000000000000000000000
As you should see, these results are identical. In fact, they are the same as what everyone has been telling you.

KSSV on 14 Jan 2022
For single precision epsilon is:
2^-23
For double precision epslon is:
2^-52
John D'Errico on 14 Jan 2022
How is the code that @KSSV shows different from what was said? What needs to be clarified in your mind?

R2021b

### Community Treasure Hunt

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

Start Hunting!