A problem with floating point precision!

1 view (last 30 days)
Mark Posen
Mark Posen on 30 Apr 2020
Commented: Mark Posen on 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 Comment
Steven Lord
Steven Lord on 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.

Sign in to comment.

Accepted Answer

Rik
Rik on 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.
  1 Comment
Mark Posen
Mark Posen on 30 Apr 2020
Many thanks Rik, 2*eps() sorted it. Clearly, a steep learning curve to climb!

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!