How to find number of significant figures in a decimal number?
    60 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Jeff
 el 21 de Jul. de 2014
  
    
    
    
    
    Comentada: Walter Roberson
      
      
 el 11 de Oct. de 2022
            For example, given a decimal number 31.4560000 I'd like to know number of significant figures in the number which is 5(3,1,4,5,6).
1 comentario
  fam fam
 el 6 de Oct. de 2017
				Bruh, 31.4560000 has 9 sig-figs
31.456 has 5 sig-figs
They are not the same number...
Respuesta aceptada
  James Tursa
      
      
 el 21 de Jul. de 2014
        
      Editada: James Tursa
      
      
 el 22 de Jul. de 2014
  
      This is not necessarily trivial to do reliably. In the first place, 31.456 is not representable in IEEE double exactly, so those trailing digits aren't really exactly all 0's anyway. E.g.,
>> num2strexact(31.456)
ans =
3.1455999999999999516830939683131873607635498046875e1
So, depending on how one prints the number out and how one sorts though all the trailing digits you can get a variety of answers. One approach might be to start with the exact string, as above, and then progressively round the last digit and re-read the result to see if you end up with the same IEEE double number. When it doesn't match, then you know you hit the smallest "significant" digit (assuming I understand what you mean by the term).
EDIT
OK, so here is a quick first cut at a function based on the idea above. Hasn't been extensively tested so caveat emptor. Seems to work for the few test cases I have thrown at it. Is not vectorized, although that could easily be done. (I hesitate to post this because I have this nagging suspicion that this could be done in a simpler way, but what the heck ...)
% SIGDIGITS returns significant digits in a numeric non-uint64 number.
% Returns "significant" digits in a number, in the sense that if you
% printed any fewer digits the reverse conversion would not equal the
% original number. I.e., the value returned from this function is the
% minimum number of digits you must print in order to recover the original
% number with a reverse conversion.
% Programmer: James Tursa
function n = sigdigits(x)
if( ~isnumeric(x) || ~isfinite(x) || isa(x,'uint64') )
    error('Need any finite numeric type except uint64');
end
if( x == 0 )
    n = 0;
    return;
end
x = abs(x);
y = num2str(x,'%25.20e'); % Print out enough digits for any double
z = [' ' y]; % Pad beginning to allow rounding spillover
n = find(z=='e') - 1; % Find the exponent start
e = n;
while( str2double(y) == str2double(z) ) % While our number is still equal to our rounded number
    zlast = z;
    c = z(e); % Least significant printed digit
    if( c == '.' )
        e = e - 1;
        c = z(e);
    end
    z(e) = '0'; % 0 the least significant printed digit
    e = e - 1;
    if( c >= '5' ) % Round up if necessary
        c = z(e);
        if( c == '.' )
            e = e - 1;
            c = z(e);
        end
        while( true ) % The actual rounding loop
            if( c == ' ' )
                z(e) = '1';
                break;
            elseif( c < '9' )
                z(e) = z(e) + 1;
                break;
            else
                z(e) = '0';
                e = e - 1;
                c = z(e);
                if( c == '.' )
                    e = e - 1;
                    c = z(e);
                end
            end
        end
    end
end
n = n - 1;
z = zlast(1:n); % Get rid of exponent
while( z(n) == '0' ) % Don't count trailing 0's
    n = n - 1;
end
n = n - 2; % Don't count initial blank and the decimal point.
end
3 comentarios
  Richard Kass
 el 22 de En. de 2019
				This is great, but what about for numbers with trailing zeroes? For instance, we know that 1.050 has 4 significant figures, but this function will say that 1.050 has 3 significant figures. I'm fairly sure that it reads in 1.050 as '1.05' when it converts to a string, but don't know how to get around this.
  Walter Roberson
      
      
 el 22 de En. de 2019
				Input coded as 1.050 is converted to binary double by MATLAB, which has no way of distinguishing that from input coded as 1.05 or 1.050000000 . 
To distinguish 1.050 from 1.05 you would need to input as character or as string object.
Más respuestas (1)
  Javier Martin
 el 10 de Oct. de 2022
        A trick:
number = -0000000031.4560000;
nDecimals = numel(extractAfter(num2str(abs(number)), '.'));
nIntegers = numel(extractBefore(num2str(abs(number)), '.'));
nFigures = nDecimals + nIntegers;
disp("Number of figures in " + num2str(number) + ": " + num2str(nFigures))
1 comentario
  Walter Roberson
      
      
 el 11 de Oct. de 2022
				As indicated by @fam fam and @Richard Kass when you have explicit trailing 0s in the number, then they are considered significant digits. 
Ver también
Categorías
				Más información sobre Elementary Math en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





