How to perform division correctly?
이전 댓글 표시
Recenty I've came across this dilemma when dealing with divisions. Actually when performing the division of a number by a product of others number I expect that the following two codes returns the same result:
a = 1 / (x * y);
b = 1 / x / y;
However, comparing the two results, it returns false logical answer.
Besides the error is in the order of esp (10^-16), on the long run it made my simulation numerically unstable.
Which sintax I am expect to use? Anyone experienced something similar?
I leave my own code to reproduce the behavior!
a = 72000 / 12 / ( 1 - 0.3 ^ 2 );
b = 72000 / (12 * ( 1 - 0.3 ^ 2 ));
a == b % false
% eps = 2.2204e-16
rel_err = (a-b)/a; % about -1.38e-16
댓글 수: 1
the cyclist
2022년 1월 23일
Not directly related to what you are seeing here, but also be aware that the slash represents the mrdivide operation, not simple division. It matters when you are dealing with vectors and matrices.
채택된 답변
추가 답변 (1개)
No division here:
a = 3*0.3;
b = 0.9;
a == b
rel_err = (a-b)/a
abs_err = abs(a-b)
카테고리
도움말 센터 및 File Exchange에서 Number Theory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!