## 从开根说起

$$y - y_1 = f^\prime (x_1) ( x - x_1 )$$

$f ^ \prime (x) = 2x$ ，因此对于本问题的牛顿迭代公式为：

$$x_n = x_{n-1} - \dfrac{y_{n-1}}{2x_{n-1}}$$

• $(x_1, y_1) = (2,2)$ , $x_2 = 2 - \dfrac{2}{4} = \dfrac{3}{2}$
• $( x_2, y_2 ) = (\dfrac{3}{2}, \dfrac{1}{4}), x_3 = \dfrac{3}{2} - \dfrac{1}{4} \cdot \dfrac{1}{3} = \dfrac{17}{12}$
• $(x_3, y_3) = (1.41666666667, 0.00694444444)， y_x = 1.41421568628$

• $x_0 = 1.41421356237$
• $y_4 = 1.41421568628$

double sqrt(double n) {
const double delta = 1E-10;
double x = 1;
while (true) {
double xn = (x + n / x) / 2;
if (abs(x - xn) < delta) {
break;
}
x = xn;
}
return 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 fuck?
y  = * ( float * ) &i;
y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//  y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

#ifndef Q3_VM
#ifdef __linux__
assert( !isnan(y) ); // bk010122 - FPE?
#endif
#endif
return y;
}