A problem with floating point precision!
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Steven Lord
le 30 Avr 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.
Réponse acceptée
Rik
le 30 Avr 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!