MatLab single precision epsilon versus double precision epsilon

조회 수: 27 (최근 30일)
Aarav Shah
Aarav Shah 2022년 1월 14일
댓글: John D'Errico 2022년 1월 14일
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
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)
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.

추가 답변 (1개)

KSSV
KSSV 2022년 1월 14일
For single precision epsilon is:
2^-23
For double precision epslon is:
2^-52
  댓글 수: 6
Aarav Shah
Aarav Shah 2022년 1월 14일
That is not solving anything in particular. Your provided code yields the same answer as I have yet you’re reporting another. I am rather confused by this. If you could fully clarify, that would help quite a bit.
John D'Errico
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by