-1

I have a problem with matlab and i'm almost sure I've seen it before somewhere and gone around it, but now I want to solve it... When I'm doing a simple calculation:

>> 14336*0.005

You'll get the expected answer:

ans = 71.6800

but when using format long:

format long; ans = 71.680000000000007

It doesn't seem to be a problem for matlab:

ans==71.68 = 1

But if I do one more calculation:

ans/0.005 = 1.433600000000000e+04
ans==14336 = 0
ans-14336 = 1.818989403545856e-12

Do anyone know a solution to get rid of the difference of 1.81e-12 of the original value and final value? I guess it's due to datatypes and stuff but is there a solution?

//Lucas

  • Solution to what? I don't think you state clearly what the issue is... – kkuilla Feb 10 '15 at 15:09
  • 1
    Welcome to the world of floating point arithmetic. Please see the duplicate answer I have linked (Why is 24.0000 not equal to 24.0000) for more details. – rayryeng Feb 10 '15 at 15:15

1 Answers1

0

Yes, the solution is not to do equality test on floating-point precision numbers. Instead, compare the absolute value of the difference to a small threshold value (e.g. eps). See the MATLAB FAQ.

am304
  • 13,758
  • 2
  • 22
  • 40