MatLab single precision epsilon versus double precision epsilon
24 views (last 30 days)
Show older comments
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.
Accepted Answer
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)
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')
eps('single')*2^23
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)
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)
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)
Epsilon here is the value produced by the SINGLE precision computations.
fprintf('%0.55f',2^-23)
fprintf('%0.55f',eps('single'))
As you should see, these results are identical. In fact, they are the same as what everyone has been telling you.
0 Comments
More Answers (1)
KSSV
on 14 Jan 2022
For single precision epsilon is:
2^-23
For double precision epslon is:
2^-52
6 Comments
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?
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!