MatLab single precision epsilon versus double precision epsilon
29 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
John D'Errico
2022-1-14
编辑:John D'Errico
2022-1-14
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 个评论
更多回答(1 个)
KSSV
2022-1-14
For single precision epsilon is:
2^-23
For double precision epslon is:
2^-52
6 个评论
John D'Errico
2022-1-14
How is the code that @KSSV shows different from what was said? What needs to be clarified in your mind?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!