필터 지우기
필터 지우기

Two numbers that should be equal are not, fractional part showing different values after 17th fractional digit

조회 수: 2 (최근 30일)
Hello everyone,
While working on a project I came accross a problem that I don't understand, so I would very much appreciate your help.
I've created a script to demonstrate the issue. The script, by itself, doesn't do anything meaningful, it's just taken out of context to reproduce the problem.
font_size = 72;
d = 0.01;
[h,w,hm,wm,cd,sp,ld] = genTLparams(font_size);
hf = sp;
vf = sp + h;
fs = 's';
fs_found = [];
fs_sp = containers.Map;
fs_sp(fs) = cell(0);
fs_ep = containers.Map;
fs_ep(fs) = cell(0);
hf = hf + w + cd;
fs_found = [fs_found fs];
fs_sp(fs) = [fs_sp(fs) {{hf vf}}];
hf = hf + sp;
hfsp = fs_sp(fs);
hfsp = hfsp{1,end}{1,1};
vfsp = fs_sp(fs);
vfsp = vfsp{1,end}{1,2};
hfsp == hf-sp
function [h,w,hm,wm,cd,sp,ld] = genTLparams(font_size)
p2mm = 0.3527777778; % point to milimeter
h = font_size*p2mm/1000;
w = 6/14*h;
hm = 10/14*h;
wm = 6/14*h;
cd = 2/14*h;
sp = 8/14*h;
ld = 22/14*h;
end
And the output is:
ans =
logical
0
I'm expecting the hfsp and (hf-sp) to have the same value, but they end up being slightly different. Their values up to the 55th fraction are:
>> sprintf('%.55f%',hfsp)
ans =
'0.0290285714304000033725294827036123024299740791320800781'
>> sprintf('%.55f%',hf-sp)
ans =
'0.0290285714304000068419764346572264912538230419158935547'
Why does this happen? What could have caused such imprecision during calculation?

채택된 답변

Stephen23
Stephen23 2020년 12월 28일
편집: Stephen23 2020년 12월 29일
"Why does this happen? What could have caused such imprecision during calculation?"
Operations on binary floating point numbers accumulate floating point error. It is the task of the programmer to understand this and to write their code such that they assume all numeric operations will accumulate floating point error. Commonly this means not testing for exact equality, instead comparing the absolute difference against a tolerance:
abs(A-B)<tol
Read more:
This is worth reading as well:

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by