浮点指数取幂算法
我必须编写一个算法,对整数或浮点参数中的基数(整数或浮点)求幂。我为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设可以使用sqrt,则可以使用以下算法:
Suppose that you can use sqrt, you can use the following algorithm:
好吧,超过 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.