What is wrong with this code for finding out the number of significant digits after decimal?
2 ビュー (過去 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 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!