Element wise multiplication .* rounding problem
古いコメントを表示
Hello,
My name is Robin and I am currently using Matlab to compute some calculations.
But I have an issue. Let's take :
M = 564;
P = sin(5);
disp((M.*10.*P) - (M.*(10.*P)));
The result should be 0. Instead I get :
ans = 9.0949e-13
I understand the gap is very small, but I think Matlab rounding algorithm should finally provide 0.
Can you help me ?
採用された回答
その他の回答 (2 件)
madhan ravi
2019 年 2 月 21 日
Second John D’Erricos answer as a workaround if you have symbolic math toolbox:
P=sym(sin(5)); % just alter your line to this
Robin L.
2019 年 2 月 21 日
1 件のコメント
John D'Errico
2019 年 2 月 21 日
Please don't add an answer just ot make a comment.
But, yes, the associative law has problems in floating point arithmetic. Both expressions are valid, yet they can yield different results in the least significant bits.
カテゴリ
ヘルプ センター および File Exchange で Linear Algebra についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!