calc digits/decimal places from resolution

匆匆过客 提交于 2019-12-23 05:49:26

问题


Is there an easy way to calculate the number of digits needed to display a floating point number with a given resolution? (Without going through string conversion)

resolution = [0.1 0.01 0.05 0.025 0.10001];

% first try
digits = -floor (log10 (resolution))

% wanted output
ex_digits = [1 2 2 3 5];

gives

digits =
   1   2   2   2   1

The first three results are fine but the other fails with my first attempt.


回答1:


You can multiply the number by powers of 10 and compare the result with its floor.

resolution = [0.1 0.01 0.05 0.025 0.10001];
k = resolution .* 10.^(1:20).';
[~, digits] = max(round(k)==k);

You may also use a tolerance to take into account precision errors:

r = round(k);
tol = eps(r) * 2;
[max_val, digits] = max(abs(r-k) < tol);
digits = max_val .* digits + ~max_val .* 20;



回答2:


Something like this seems to come close enough:

ceil( log10( [~, D] = rat( resolution, eps ) ) )


来源:https://stackoverflow.com/questions/52983209/calc-digits-decimal-places-from-resolution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!