Return largest number of decimal places in a vector of numbers
11 ビュー (過去 30 日間)
古いコメントを表示
I'm trying to return the largest number of decimals from a vector of numbers. So say if I had x=[0.1,0.12,0.123,0.1234] I'd want an output of 4.
function [y] = maxdecimals(x)
for a = 1:length(x)
b(1,a) = strlength(num2str(abs(x(1,a))-floor(abs(x(1,a)))))-2;
% Creates vector with number of decimals
end
y = max(b);
% Finds maximum decimal number within vector
end
I'm getting a problem where if the number of decimal places is greater than 5, it doesn't seem to work. E.g. for x=[0.123456789,0.1,0.12,0.123] I get an output of 5, even though I should be getting 9.
For example, I input 0.123456789 and I get 5
strlength(num2str(abs(0.123456789)-floor(abs(0.123456789))))-2
ans =
5
Is there something about num2str that doesn't work for a certain length of numbers or is it something to do with the format limiting the number of decimal places?
>> num2str(abs(0.123456789)-floor(abs(0.123456789)))
ans =
'0.12346'
0 件のコメント
回答 (4 件)
Bruno Luong
2023 年 9 月 20 日
編集済み: Bruno Luong
2023 年 9 月 20 日
All finite floating numbers when translated to decimal MUST eventually finish by 0 sequence.
x = [0.123456789,0.1,0.12,0.123];
fprintf('%.100f\n', x)
Why? because they are sum of power 2, and each power 2 can be written exactly in 10 base with finite number of digits, since 2 divides 10.
So actually the answer [9 1 2 3] for the above x is wrong, strictly speaking.
The correct answer is
x = [0.123456789,0.1,0.12,0.123];
s = arrayfun(@(x)sprintf('%.150f', x), x, 'unif', 0);
truedigitlength = cellfun(@(s) find(s~='0',1,'last')-2, s)
To get the wanted [9 1 2 3] output, one needs to specify the length for truncation and rounding in 10 base, e.g. around 15, 16 (-log10(eps)), which is kind of arbitrary definition. and likely to fail for numbers "small" x (with log10(x) << -1).
2 件のコメント
Image Analyst
2023 年 9 月 20 日
@Bruno Luong thanks for clarifying and expanding. I think this is the best and most accurate answer posted so far.
Bruno Luong
2023 年 9 月 19 日
編集済み: Bruno Luong
2023 年 9 月 19 日
Check out the second input of num2str in the doc (in general always read the doc throughly most of the time the answer is right there).
0 件のコメント
Image Analyst
2023 年 9 月 19 日
All double numbers have the same number of decimal points, unless they're rounded, because they're all 64 bit numbers. Watch:
x = [0.123456789,0.1,0.12,0.123];
fprintf('%.50f\n', x)
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!