What is wrong with this code for finding out the number of significant digits after decimal?
2 views (last 30 days)
Show older comments
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 Comments
Fangjun Jiang
on 1 Jun 2024
Edited: Fangjun Jiang
on 1 Jun 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
Answers (2)
Stephen23
on 1 Jun 2024
Edited: Stephen23
on 1 Jun 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 Comments
Image Analyst
on 1 Jun 2024
0 Comments
See Also
Categories
Find more on Bartlett in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!