1.牛顿迭代法
double sqrt(double x) {
if (x == 0) return 0;
double last = 0.0;
double res = 1.0;
while (res != last)
{
last = res;
res = (res + x / res) / 2;
}
return res;
}
2.快速开根算法,卡马克算法,精度不是很高
float SqrtByCarmack( float number )
{
int i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( int * ) &y;
i = 0x5f375a86 - ( i >> 1 );
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) );
y = y * ( threehalfs - ( x2 * y * y ) );
y = y * ( threehalfs - ( x2 * y * y ) );
return number*y;
}
牛顿迭代与卡马克快速开根算法解析
1万+

被折叠的 条评论
为什么被折叠?



