如何计算任意幂/根?

发布于 2024-08-03 18:14:31 字数 218 浏览 6 评论 0原文

我有一个应用程序需要将数字提高到分数次幂。目标平台是 FPGA,我可以估算它的 FPU 大小,但我需要一种算法来将数字提高到分数幂,只是为了进行可行性研究。我假设浮点是最坏的情况,我希望在实践中我们能够使用捷径,但现在我想表明最坏的情况可以由我们来实现。

我想我应该在这里问一下,看看是否有任何常见的方法可以检查。我知道有软件方法可以做到这一点,我想要一个相当有效的算法来开始。我会担心 FPGA 的实现。

I have a application which needs to raise a number to a fractional power. The target platform is an FPGA and I can get estimates on an FPU size for it, but I need an algorithm for raising a number to a fractional power just for a feasibility study. I'm assuming floating point as a worst case, I expect in practice we will be able to use short cuts, but for now I want to show that worst case can be implemented on our part.

Thought I'd ask here and see if there were any common methods that I could checkout. I know there are software methods of doing this, I want just a reasonably efficient algorithm to start with. I'll worry about the FPGA implementation.

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

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

发布评论

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

评论(2

叹倦 2024-08-10 18:14:31

您的输入范围是任意的,还是在某个范围内已知的?

无论哪种情况,xm = exp(m log x),因此如果您可以创建函数来计算 exp(x) 和 log(x) 并进行乘法运算,那么您可能已经准备就绪。

您必须弄清楚如何处理 x 的非正值。

(log(x) 提示:如果这是 IEEE-754 浮点,则将如有必要,直到您最终得到某个值 K 的 2k 和 2k+1 之间的数字范围。这使您可以处理 2:1 的范围,其中用多项式近似并不难。那么您只需处理指数和移位数的少量可能性即可

:写出 x = k+b,其中 0 <= b <。 ; 1 且 k 是一个整数。那么 exp(x) = exp(k)*exp(b); b 的范围有限,k 的离散可能性数量有限。)

(提示 #2:这些数字可能会更好 )对于 xm = g(mf(x)),其中 f(x) = log2x 且 g(x) = 2x。 )

Is your range of inputs arbitrary, or known within a certain range?

in either case, xm = exp(m log x), so if you can create functions to evaluate exp(x) and log(x) and have a multiply, you're probably all set.

You'll have to figure out how you want to handle nonpositive values of x.

(hint for log(x): if this is IEEE-754 floating point, shift the significand if necessary until you end up with a range of numbers between 2k and 2k+1 for some value K. This lets you deal with a 2:1 range which isn't too hard to approximate with polynomials. Then you just have a small number of possibilities to handle for the exponent and the shift number.

Corresponding hint for exp(x): write x = k+b where 0 <= b < 1 and k is an integer. Then exp(x) = exp(k)*exp(b); b has limited range and k has a limited number of discrete possibilities.)

(hint #2: the numbers probably work out better for xm = g(m f(x)) where f(x) = log2x and g(x) = 2x.)

七月上 2024-08-10 18:14:31

正如 Jason S 所说,这是使用恒等式 xm = exp(m log x) 完成的。但在实践中,您将不得不处理截断错误。
我认为通常这样做的方式是

  1. 使用这个恒等式: xm = (2n * x / 2n) m = 2nm * (x/2n)m 并找到一个整数 n,使得 1 <= x/ 2n n n 2.
  2. 计算 t = log2(x / 2n)。这可以使用足够高阶的泰勒展开式或良好的牛顿-拉夫森式来完成。您必须确保区间 [1, 2[ 上的最大误差对您来说不会太大。
  3. 计算 u = nm + tm。
  4. 我们现在的目标是计算 2u。利用 2u = 2v * 2uv 的事实并找到一个整数 v,使得 0 <= uv << 1.
  5. 再次使用我们的朋友 Taylor 或 Newton-Raphson 计算 w = 2uv。感兴趣的区间是 0 <= uv < 1.
  6. 现在您的答案是 2v * w。

As Jason S said, this is done using the identity xm = exp(m log x). In practice though, you will have to deal with truncation errors.
I think that the way this is usually done is

  1. Use this identity: xm = (2n * x / 2n)m = 2nm * (x/2n)m and find an integer n such that 1 <= x/2n < 2.
  2. Calculate t = log2(x / 2n). This can be done either using a sufficiently high degree Taylor expansion, or with good ol' Newton-Raphson. You have to make sure that the maximum error over the interval [1, 2[ is not too large for you.
  3. Calculate u = nm + tm.
  4. Our goal is now to calculate 2u. Use the fact that 2u = 2v * 2u-v and find an integer v such that 0 <= u-v < 1.
  5. Calculate w = 2u-v, again using our friend Taylor or Newton-Raphson. The interval of interest is 0 <= u-v < 1.
  6. Your answer is now 2v * w.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文