Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Stephen23
Stephen23 el 28 de Dic. de 2020
Editada: Stephen23 el 29 de Dic. de 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:

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by