浮点指数取幂算法

发布于 2024-10-07 02:39:52 字数 365 浏览 5 评论 0原文

我必须编写一个算法,对整数或浮点参数中的基数(整数或浮点)求幂。我为 Deluge (zoho.com) 编写了这个算法,但它只能使用整数指数:

float math.potencia(float base, int expoente)
{  
   if(expoente>0)
    {
    base = base * thisapp.math.potencia(base, (input.expoente  -  1));
    }
    else if (expoente == 0)
    {
    base = 1;
    }
    return base;
}

(Deluge 没有增强运算符或函数)。谢谢!

I must to write a algorithm that exponentiates a base (integer or float) in a integer or float argument. I wrote this algorithm for Deluge (zoho.com), but it can only use integer exponents:

float math.potencia(float base, int expoente)
{  
   if(expoente>0)
    {
    base = base * thisapp.math.potencia(base, (input.expoente  -  1));
    }
    else if (expoente == 0)
    {
    base = 1;
    }
    return base;
}

(Deluge doesn't have a potentiation operator or function). Thanks!

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

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

发布评论

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

评论(2

巡山小妖精 2024-10-14 02:39:52

假设可以使用sqrt,则可以使用以下算法:

double EPS = 0.0001;

double exponentiation(double base, double exp) {
  if(exp >= 1) {
    double temp = exponentiation(base, exp / 2);
    return temp * temp;
  } else {
    double low = 0;
    double high = 1.0;

    double sqr = sqrt(base);
    double acc = sqr;    
    double mid = high / 2;

    while(fabs(mid - exp) > EPS) {
      sqr = sqrt(sqr);

      if (mid <= exp) {
          low = mid;
          acc *= sqr;
      } else {
          high = mid;
          acc *= (1 / sqr);
      }

      mid = (low + high) / 2;
    }

    return acc;
  }
}

Suppose that you can use sqrt, you can use the following algorithm:

double EPS = 0.0001;

double exponentiation(double base, double exp) {
  if(exp >= 1) {
    double temp = exponentiation(base, exp / 2);
    return temp * temp;
  } else {
    double low = 0;
    double high = 1.0;

    double sqr = sqrt(base);
    double acc = sqr;    
    double mid = high / 2;

    while(fabs(mid - exp) > EPS) {
      sqr = sqrt(sqr);

      if (mid <= exp) {
          low = mid;
          acc *= sqr;
      } else {
          high = mid;
          acc *= (1 / sqr);
      }

      mid = (low + high) / 2;
    }

    return acc;
  }
}
是你 2024-10-14 02:39:52

好吧,超过 17 个小时没有回复,我终于找到了自己问题的答案:

以最简单的方式,我们可以使用“e”的值除以指数的对数求幂来解决问题:

e^(Log(number)/index)

其中 number 是被数,index 是所需的根。

例如:数字 1024 的 10 次方根:
e^(Log(1024)/10) = 2。

PS:Log函数的底也是“e”。
“e”的四舍五入值为:2.718281828459045

我希望这项技术对您有用。

Well, more than 17 hours without a reply, finally I've found an answer to my own question:

In the simplest way, we can solve the problem using the value of "e" exponentiating to the logarithm of the number divided by the index:

e^(Log(number)/index)

where number is the radicand and index is the desired root.

Eg: The 10th root of the number 1024:
e^(Log(1024)/10) = 2.

PS: the base of the Log function is also "e".
the rounded value for "e" is: 2.718281828459045

I hope this technique may be usefull for you.

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