What is wrong with this code for finding out the number of significant digits after decimal?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have created this code for finding the number of siignificant digits after decimal for Cody, but for some reason, it shows error (semantic). Can anyone please help identify why? Thank you.
The error comes when I apply:
x = [1.000 1.04 0.22 10.1; 2.05 2.33 4.1 1000.31; 5.00010 6.429 7.492 8.0]
I tried debugging, but the error comes due to 6.429 having a '1' after another 11 zeroes.
Code:
function y = find_max_sigdec(x)
stop = 0;
i = 0;
if x == fix(x)
y = 0;
else
while stop == 0
x = x*10;
i = i+1;
if x == fix(x)
y = i;
stop = 1;
end
end
end
end
3 Commenti
Fangjun Jiang
il 1 Giu 2024
Modificato: Fangjun Jiang
il 1 Giu 2024
You need to change the type of your input then process it.
x = ["1.000" "1.04" "0.22"]
y=6.429
format long
y
Risposte (2)
Stephen23
il 1 Giu 2024
Modificato: Stephen23
il 1 Giu 2024
The least unexpected number of non-zero fractional digits aka decimal places (limited to 15 significant figures of the binary floating point numbers):
format long G
x = [1.000, 1.04, 0.22, 10.1; 2.05, 2.33, 4.1, 1000.31; 5.00010, 6.429, 7.492, 8.0]
str = compose("%.15e",x(:));
tkn = regexp(str,'^\d+\.(\d*[1-9])?0*[eE]((+|-)\d+)$','tokens','once');
tkn = vertcat(tkn{:});
dcp = strlength(tkn(:,1))-str2double(tkn(:,2));
dcp = max(0,reshape(dcp,size(x)))
0 Commenti
Image Analyst
il 1 Giu 2024
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!