Numerical precision in subtracting two almost identical variables?
5 ビュー (過去 30 日間)
古いコメントを表示
As far as I understand it, Matlab uses 16 digits of precision. However, I have a program that subtracts two variables and very often gives results much smaller than 1e-16. For example. When asked it displays a=0.000000006129562, b=0.000000006129562 but a-b=1.0e-24 * 0.827180612553028 How does it distinguish two variables that differ by a quantity much smaller than the 15th decimal place, if it doesn't have an internal representation of it no larger than 16 digits? Shouldn't be it a-b=0 always?
1 件のコメント
Stephen23
2024 年 2 月 28 日
編集済み: Stephen23
2024 年 2 月 28 日
In your question you incorrectly conflate two related but different topics. Here, I highlighted them for you:
"How does it distinguish two variables that differ by a quantity much smaller than the 15th decimal place, if it doesn't have an internal representation of it no larger than 16 digits?"
Decimal places is not the same thing as significant digits. Mixing up those two different things (like you are doing) is not going to help you do any computational mathematics, let alone do any science. There are many many many tutorials online which explain the difference, so I will not waste my time duplicating tutorial content here.
"Shouldn't be it a-b=0 always?"
No.
回答 (1 件)
Aquatris
2024 年 2 月 28 日
Here is your answer link
Its all in the low level things. If you display your values with 'format hex', you will see they differ by 1 bit, and that bits gives you the 1e-16 difference.
Example:
x = 0.6;
y = 6*0.1;
delta = x-y
format long
[x y]
format hex
[x y]
6 件のコメント
Stephen23
2024 年 2 月 28 日
編集済み: Stephen23
2024 年 2 月 28 日
Use SPRINTF (results depend on the OS / MATLAB version)
Or use NUM2STREXACT: https://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str
Note that digits after the 16-17th have no significance.
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!