How to get around round off error?
Mostrar comentarios más antiguos
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?
11 comentarios
Guillaume
el 8 de Jul. de 2019
The answer should be 1e-7
Well, for a start, 1e-7 cannot be stored exactly on any computer that uses ieee double representation.
The answer is 1e-7 for all intent and purposes. It only differs by about 6e-19 which is just noise compared to 1e-7.
The fact that you write 0.02155770000000 instead of 0.0215577 would indicate that you missed the point of what you were reading. You will always get roundoff error on a computer but unless you run into issues such as catastrophic cancellation, that roundoff error is negligible.
Indeed, the only way to avoid any kind of roundoff is to go the symbolic way, which is orders of magnitude slower, and completely overkill in most situations. Of course, if you convert from symbolic back to numerical, you'll go back to having roundoff.
Rather than trying to eliminate roundoff, just accept that it's there. It's also present in the physical world.
ARSHA MAMOOZADEH
el 8 de Jul. de 2019
Walter Roberson
el 8 de Jul. de 2019
sym('.0215577','d')
Will get you a bit closer, but it turns out that symbolic floating point is not really stored internally as decimal, the internal base appears to be 2^30. This is mostly not noticeable but differences against decimal representation can be found if you push the system hard enough.
ARSHA MAMOOZADEH
el 8 de Jul. de 2019
Walter Roberson
el 8 de Jul. de 2019
Sorry, I should have said
sym('.0215577')
This is not the same as
sym(.0215577)
or
sym(.0215577,'d')
ARSHA MAMOOZADEH
el 8 de Jul. de 2019
Editada: ARSHA MAMOOZADEH
el 8 de Jul. de 2019
Guillaume
el 8 de Jul. de 2019
F1=.0215577
You already have roundoff error at this point. The number .0215577 cannot be stored exactly. The actual number that is stored is 2.155769999999999908535386339281103573739528656005859375e-2
ARSHA MAMOOZADEH
el 8 de Jul. de 2019
Walter Roberson
el 8 de Jul. de 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
el 8 de Jul. de 2019
Stephen23
el 8 de Abr. de 2021
"16 is the maximum number of decimals my input has."
Then any attempt to define a higher precision number for your operations is totally meaningless.
I very much doubt that your input data were even measured to 16 significant digit accuracy, given that some of the most accurate measurements in science are around the same order:
Respuestas (1)
Zakaria moeri
el 8 de Abr. de 2021
0 votos
take a look at vpa and digits method already exist in matlab documentation.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!