A problem with floating point precision!
3 ビュー (過去 30 日間)
古いコメントを表示
Hello all,
I'm very new to MATLAB, so please forgive this very basic question!
I have run up against an issue with floating-point precision and hope that someone can help!
>> (1.12*100)-fix(1.12*100)
ans =
1.421085471520200e-14
>>(112)-fix(112)
ans =
0
Clearly the second answer is what I was expecting, and the first answer is not giving the same result because of floating point precision (I assume).
Any advice as to how to overcome this (since my code needs to multiply numbers by powers of 10) is much appreciated.
Thanks,
Mark
1 件のコメント
Steven Lord
2020 年 4 月 30 日
Depending on what your code is doing, a change of units may also help. As an example if you were working with currency, rather than working in units of dollars:
x_dollar = 0.1;
y_dollar = x_dollar + x_dollar + x_dollar;
y_dollar - 0.3
maybe work in units of cents:
x_cents = 10;
y_cents = x_cents + x_cents + x_cents;
y_cents - 30
For distances, use centimeters or millimeters instead of fractions of a meter.
採用された回答
Rik
2020 年 4 月 30 日
Don't compare to 0, but compare the absolute difference to a tolerance.
a=1.12*100;
b=fix(a);
abs(a-b)<=eps(a)
If you want to be safe you can do 2*eps.
There are several functions that will allow you to use a tolerance: ismembertol and uniquetol are two of the more usefull ones.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!