public static double sqrt(double c)
{
if (c < 0) return Double.NaN;
double t = c; // line 1
double err = 1e-15; // line 2
while (Math.abs(t - c/t) > err * t) // line 3
t = (c/t + t) / 2.0; // line 4
return t;
}
Q1: I am confused by the variable t in line1 and line 4: since t = c, then c/t = 1, what does line 4 mean?
Q2: In line 3, what's the purpose to check?
I searched "Newton's method" and got several explanation, but I still could not understand. May I request an straight forward explanation here?