[英]Is there any function for square root of another order?
由于大量数字的乘积可能变得过大,因此通常最好将几何平均值计算为数字对数的普通算术平均值的指数。
C ++标准库提供自然对数的log
和exp
用于指定基数e 。 从而:
#include <math.h>
auto geometric_mean( double const* const first, double const* const past_end )
-> double
{
double sum = 0;
for( double const* p = first; p != past_end; ++p )
{
sum += log( *p );
}
const int n = past_end - first;
return exp( sum/n );
}
您可以使用std::accumulate
代替循环。
免责声明:编码器手中没有触及代码。
你可以做这个pow(x, 1.0/m)
。 与m相同。 根。
使用以下内容:
#include <cmath>
...
double nth_root = std::pow(number, 1.0 / n);
https://en.wikipedia.org/wiki/Nth_root
m
是根的程度,根据它的产品是radicand。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.