从分辨率计算数字/小数位 [英] calc digits/decimal places from resolution

查看:116
本文介绍了从分辨率计算数字/小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来计算以给定分辨率显示浮点数所需的位数? (无需进行字符串转换)

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];

给予

digits =
   1   2   2   2   1

前三个结果很好,但另一个失败,都是我的第一次尝试.

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

推荐答案

您可以将数字乘以10的幂,然后将结果与其下限进行比较.

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;

这篇关于从分辨率计算数字/小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆