MATLAB Answers

How to get around round off error?

9 views (last 30 days)
ARSHA MAMOOZADEH
ARSHA MAMOOZADEH on 8 Jul 2019
Hello all, I read about the round off error that MATLAB experiences with double precision numbers, and it being caused by how MATLAB stores numbers in binary. I was wondering if there was any way to get around this. Specifically I would like to subtract 0.021557700000000 from 0.021557800000000 without any round off error. The answer should be 1e-7 but MATLAB gives -9.999999999940612e-08. I tried to convert the numbers to symbols and subtract symbolically but I got the same error. Any ideas?

  10 Comments

Show 7 older comments
ARSHA MAMOOZADEH
ARSHA MAMOOZADEH on 8 Jul 2019
My bad, thank you for your help.
Walter Roberson
Walter Roberson on 8 Jul 2019
Right.
If you know the number of decimal places involved then you can do something like
round(F1*1e7) - round(F2*1e7)
and the answer would then be 1e7 times too large. However, you cannot just divide through by 1e7 without introducing round-off error.
ARSHA MAMOOZADEH
ARSHA MAMOOZADEH on 8 Jul 2019
Yep. Currently I am testing this out...
A=(215577/10000000);
B=(215578/10000000);
C=(215579/10000000);
BA=B-A;
CB=C-B;
BA_eps=eps(BA)
CB_eps=eps(CB)
if BA_eps == CB_eps
B=C;
A=C;
end
This seems to be working, and I think I am going to add a tolerance on the if statement. Another thing I made was this however it still has the round off error.
F21=round(F2-F1,16)
F32=round(F3,F2,16)
16 is the maximum number of decimals my input has.

Sign in to comment.

Answers (0)