A problem with floating point precision!
조회 수: 7 (최근 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!