Two numbers that should be equal are not, fractional part showing different values after 17th fractional digit
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Aleksandar Ritan
il 28 Dic 2020
Modificato: Stephen23
il 29 Dic 2020
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?
Risposta accettata
Stephen23
il 28 Dic 2020
Modificato: Stephen23
il 29 Dic 2020
"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:
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!