如何实现无n次方有理数的幂运算?

发布于 2024-10-07 16:34:09 字数 152 浏览 0 评论 0原文

它对我来说只有 log(base "e")、sin、tan 和 sqrt(仅平方根)函数和基本算术运算符 (+ - * / mod)。我还有“e”常数。

我正在尝试使用 Deluge (zoho.com) 来解决这些限制的几个问题。我必须实现有理(分数)底数和指数的幂运算。

Its available for me only log(base "e"), sin, tan and sqrt (only square root) functions and the basic arithmetical operators (+ - * / mod). I have also the "e" constant.

I'm experimenting several problems with Deluge (zoho.com) for these restrictions. I must implement exponentiation of rational (fraction) bases and exponents.

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

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

发布评论

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

评论(2

谈下烟灰 2024-10-14 16:34:09

假设您要计算 pow(A, B)

考虑以 2 为基数的 B 表示形式:

B = b[n]   * pow(2, n    ) +
    b[n-1] * pow(2, n - 1) +
    ...
    b[2]   * pow(2, 2    ) +
    b[1]   * pow(2, 1    ) +
    b[0]   * pow(2, 0    ) +
    b[-1]  * pow(2, -1   ) +
    b[-2]  * pow(2, -2   ) +
    ...

 = sum(b[i] * pow(2, i))

其中 b[x] 可以是 01pow(2, y) 是 2 的整数幂(即 1, 241/21/41/8)。

然后,

pow(A, B) = pow(A, sum(b[i] * pow(2, i)) = mul(pow(A, b[i] * pow(2, i)))

因此可以仅使用乘法和平方根运算来计算pow(A, B)

Say you want to calculate pow(A, B)

Consider the representation of B in base 2:

B = b[n]   * pow(2, n    ) +
    b[n-1] * pow(2, n - 1) +
    ...
    b[2]   * pow(2, 2    ) +
    b[1]   * pow(2, 1    ) +
    b[0]   * pow(2, 0    ) +
    b[-1]  * pow(2, -1   ) +
    b[-2]  * pow(2, -2   ) +
    ...

 = sum(b[i] * pow(2, i))

where b[x] can be 0 or 1 and pow(2, y) is an integer power of two (i.e., 1, 2, 4, 1/2, 1/4, 1/8).

Then,

pow(A, B) = pow(A, sum(b[i] * pow(2, i)) = mul(pow(A, b[i] * pow(2, i)))

And so pow(A, B) can be calculated using only multiplications and square root operations

冰葑 2024-10-14 16:34:09

如果你有一个执行 e^x 的函数 F(),其中 e 是常数,x 是任意数字,那么你可以这样做:(a 是底​​数,b 是指数,ln 是 log-e)

a^b = F(b * ln(a))

如果没有执行 e^x 的 F(),那么事情会变得更棘手。如果您的指数 (b) 是有理数,那么您应该能够使用某种循环找到整数 m 和 n,使得 b = m/n。一旦有了 m 和 n,就进行另一个循环,将 a 乘以 m 次得到 a^m,然后将 a 乘以 n 次得到 a^n,然后除 a^m/a^n 得到 a^ (m/n),即a^b。

If you have a function F() that does e^x, where e is the constant, and x is any number, then you can do this: (a is base, b is exponent, ln is log-e)

a^b = F(b * ln(a))

If you don't have F() that does e^x, then it gets trickier. If your exponent (b) is rational, then you should be able to find integers m and n so that b = m/n, using a loop of some sort. Once you have m and n, you make another loop which multiples a by itself m times to get a^m, then multiples a by itself n times to get a^n, then divide a^m/a^n to get a^(m/n), which is a^b.

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