许多资料表明,众所周知的快速平方根反算法可以推广到计算任意幂的平方根。不幸的是,我还没有找到这样的C ++实现,而且我也不擅长通过数学自己概括这种方法。您能帮助我做到这一点,还是提供现成的解决方案?我认为这对许多人都是有用的,尤其是在有充分解释的情况下。
这是原始算法,例如,我不太了解需要更改的内容1 /cbrt(x):
float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the...?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y …Run Code Online (Sandbox Code Playgroud) 如何专门化union类型模板?假设我有模板功能
template <typename T>
void foo(T value);
Run Code Online (Sandbox Code Playgroud)
如果T不是任何union类型,我想禁止调用此函数。我该如何实现?