How to get around round off error?
古いコメントを表示
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 件のコメント
Guillaume
2019 年 7 月 8 日
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
2019 年 7 月 8 日
Walter Roberson
2019 年 7 月 8 日
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
2019 年 7 月 8 日
Walter Roberson
2019 年 7 月 8 日
Sorry, I should have said
sym('.0215577')
This is not the same as
sym(.0215577)
or
sym(.0215577,'d')
ARSHA MAMOOZADEH
2019 年 7 月 8 日
編集済み: ARSHA MAMOOZADEH
2019 年 7 月 8 日
Guillaume
2019 年 7 月 8 日
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
2019 年 7 月 8 日
Walter Roberson
2019 年 7 月 8 日
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
2019 年 7 月 8 日
Stephen23
2021 年 4 月 8 日
"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:
回答 (1 件)
Zakaria moeri
2021 年 4 月 8 日
0 投票
take a look at vpa and digits method already exist in matlab documentation.
カテゴリ
ヘルプ センター および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!