问题
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