17 * Returns the square root of x.
19 * Range reduction involves isolating the power of two of the
20 * argument and using a polynomial approximation to obtain
21 * a rough value for the square root. Then Heron's iteration
22 * is used three times to converge to an accurate value.
30 * arithmetic domain # trials peak rms
31 * DEC 0, 10 60000 2.1e-17 7.9e-18
32 * IEEE 0,1.7e308 30000 1.7e-16 6.3e-17
37 * message condition value returned
38 * sqrt domain x < 0 0.0
43 Cephes Math Library Release 2.8: June, 2000
44 Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
50 extern double frexp ( double, int * );
51 extern double ldexp ( double, int );
53 double frexp(), ldexp();
55 extern double SQRT2; /* SQRT2 = 1.41421356237309504880 */
69 mtherr( "sqrt", DOMAIN );
73 /* separate exponent and significand */
79 e = ((*q >> 7) & 0377) - 0200;
85 /* Note, frexp and ldexp are used in order to
86 * handle denormal numbers properly.
93 e = ((*q >> 4) & 0x0fff) - 0x3fe;
103 e = ((*q >> 4) & 0x0fff) - 0x3fe;
110 /* approximate square root of number between 0.5 and 1
111 * relative error of approximation = 7.47e-3
113 x = 4.173075996388649989089E-1 + 5.9016206709064458299663E-1 * z;
115 /* adjust for odd powers of 2 */
119 /* re-insert exponent */
121 x = ldexp( x, (e >> 1) );
124 *q += ((e >> 1) & 0377) << 7;
128 x = ldexp( x, (e >> 1) );
130 *q += ((e >>1) & 0x7ff) << 4;
135 x = ldexp( x, (e >> 1) );
137 *q += ((e >>1) & 0x7ff) << 4;
142 /* Newton iterations: */
149 /* Note, assume the square root cannot be denormal,
150 * so it is safe to use integer exponent operations here.