What is wrong with this code for finding out the number of significant digits after decimal?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Fangjun Jiang
2024-6-1
编辑:Fangjun Jiang
2024-6-1
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
回答(2 个)
Stephen23
2024-6-1
编辑:Stephen23
2024-6-1
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 个评论
Image Analyst
2024-6-1
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Bartlett 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!