Search code examples
matlabrounding-error

Matlab: finding position of last nonzero digit of a number


I have a column vector pp filled with numbers, for instance:

23.234000
3.1237340
4.4359000

I want to find the number of places to the right of the decimal that the smallest nonzero digit in the vector occupies, which would in this case be 6, because of the 4 in 3.123734. Then I want to multiply every number in the vector by 10^6, to get rid of all decimals in the vector. I want to do this to eliminate rounding errors. What's the best way to get this done?


Solution

  • The actual value may be different from what is actually being displayed by MATLAB. For instance, consider the following example:

    >> x = 1.4 - [0.1; 0.09999999]
    
    x =
       1.3000
       1.3000
    

    MATLAB shows both values to be 1.3, but in fact, none of them is:

    >> x - 1.3
    
    ans =
      -2.2204e-16
       1.0000e-08
    

    My suggestion is therefore to decide on a fixed accuracy (say, 6 digits), and then multiply by the corresponding power of 10.