四舍五入到 10 的幂

发布于 2024-11-25 08:40:59 字数 259 浏览 3 评论 0原文

我有一个变量 tauMax,我想将其四舍五入到最接近的 10 次方(1、10、100、1000...)。我使用下面的表达式来查找 tau 数组中最接近最大值的整数。我找到最大值是因为我正在尝试计算 10 的幂,这应该是 x 轴的截止值。因此,tauMax 等于 756,因此我想要一个输出 1000 或 3(对于 10^3)的表达式。

tauMax = round(max(tau));

我真的很感激任何帮助!

I have a variable, tauMax, that I want to round up to the nearest power of ten(1, 10, 100, 1000...). I am using the below expression to find the closest integer to the max value in the tau array. I am finding the max value because I am trying to calculate the power of ten that should be the x axis cutoff. In this cause, tauMax is equal to 756, so I want to have an expression that outputs either 1000, or 3(for 10^3).

tauMax = round(max(tau));

I'd really appreciate any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千秋岁 2024-12-02 08:40:59

由于您正在讨论以 10 为基数,因此您可以使用 log10 来获取位数。

怎么样:

>> ceil(log10(756))

ans =

     3

Since you're talking base 10, you could just use log10 to get the number of digits.

How about:

>> ceil(log10(756))

ans =

     3
一影成城 2024-12-02 08:40:59

我并不真正使用 Matlab,但用我所知道的任何语言执行此操作的通常方法是:取以 10 为底的对数,然后将该数字向上舍入到最接近的整数,然后计算 10 的该数字的幂。在Python中:

from math import ceil, log

def ceil_power_of_10(n):
    exp = log(n, 10)
    exp = ceil(exp)
    return 10**exp

>>> print(ceil_power_of_10(1024))  # prints 10000

I don't really do Matlab, but the usual way to do this in any language I do know is: take the logarithm base 10, then round up that number to the nearest integer, then compute 10 to the power of that number. In Python:

from math import ceil, log

def ceil_power_of_10(n):
    exp = log(n, 10)
    exp = ceil(exp)
    return 10**exp

>>> print(ceil_power_of_10(1024))  # prints 10000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文