A problem with floating point precision!
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mark Posen
am 30 Apr. 2020
Kommentiert: Mark Posen
am 30 Apr. 2020
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 Kommentar
Steven Lord
am 30 Apr. 2020
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.
Akzeptierte Antwort
Rik
am 30 Apr. 2020
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!