0

In a previous problem, there was the equation:

e = 1 – 3*((4/3) – 1) 

And the is not 0, but rather:

e =2.2204e-16

I understand why this occures, for 4/3 cannot be represented exactly.

The closest representation of 4/3 is:

1.3333333333333332593184650249895639717578887939453125

And after the necessary operations, e =2.2204e-16 is obtained.

Now, I am having trouble understanding the odd behaviors in these equations involving e:

1)

 b = 1e-16 + 1 - 1e-16; 

 c = 1e-16 - 1e-16 + 1;

Here we expect b to equal c, but this isn't the case. I know there is something fishy going on with the order of operations and the error, but I don't exactly understand why b doesn't equal c.

2)

sqrt(1e-16 + 1) - 1;
ans=0

Now, I suspect when the subtraction is done, and since the operands are nearly equal, there was some sort of cancellation. But I am still a little confused and would appreciate a detailed explanation. Thank you.

Chad
  • 185
  • 1
  • 11

1 Answers1

0

When working with floating point numbers even in MATLAB it's not accurate when it comes to comparing such values. Assertions for near equality, using tolerance (relative error, absolute error etc.) and so on is what one should do if a specific level of accuracy is required.

Check Comparing Floating Point Numbers, 2012 Edition as well as here. In MATLAB relative error accuracy can be achieved through the eps function.

Community
  • 1
  • 1
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161