1

I have a question about the precision in Matlab. I searched the questions here and they mention that there are precision problems when using very large or very small numbers and other posts looking at the comparison of numbers. My case is a bit different

I do not have that, my numbers have only 4 decimal places and I want them to add up to one. Example: Initial data:

aeb =
0.231215677537590   0.470472652172102   0.203009018534716   
0.087759104877338   0.007588684370711

Then I rounded it to get 4 decimals:

aeb = round(aeb*10000)/10000
0.231200000000000   0.470500000000000   0.203000000000000   
0.087800000000000   0.007600000000000

Then I identify the largest number and replace it by the difference with one

[~, idx]=max(aeb);
aeb(idx)=0;
aeb(idx)= 1 - sum(aeb);

But when I then do:

1 - sum(aeb)
1.110223024625157e-16

Does anyone know how I can make them add to one ? I just want 3-5 decimal places.

user3804227
  • 135
  • 8
  • 3
    Numbers that are order 10^-16 are effectively zero in Matlab (and indeed, all floating point operations). See here: http://stackoverflow.com/q/686439/2917957 – David Nov 26 '15 at 23:41
  • _Does anyone know how I can make them add to one?_ You can't; at least not with `double` data type. But the difference is in the 15th-16th decimal, so it shouldn't be a problem? – Luis Mendo Nov 26 '15 at 23:47
  • @David: As stated, that is not correct. Not all operations and it's only true with respect to 1. – horchler Nov 27 '15 at 00:48

1 Answers1

3

Their sum is within machine epsilon of the number one. The short answer is that any difference that small is indistinguishable from zero.

A practical point to be aware of....

In a lot of computing contexts, you don't want to test for equality, you want to test if the difference is within a certain tolerance. Eg.

abs(x - y) < tol
Community
  • 1
  • 1
Matthew Gunn
  • 4,451
  • 1
  • 12
  • 30
  • Thanks a lot. It is true, I can just replace any number smaller than 10^-15 with zero and then all my numbers add to one. – user3804227 Nov 27 '15 at 02:33