2 /* @(#)z_sqrt.c 1.0 98/08/13 */
3 /*****************************************************************
4 * The following routines are coded directly from the algorithms
5 * and coefficients given in "Software Manual for the Elementary
6 * Functions" by William J. Cody, Jr. and William Waite, Prentice
8 *****************************************************************/
12 <<sqrt>>, <<sqrtf>>---positive square root
21 double sqrt(double <[x]>);
22 float sqrtf(float <[x]>);
25 <<sqrt>> computes the positive square root of the argument.
28 On success, the square root is returned. If <[x]> is real and
29 positive, then the result is positive. If <[x]> is real and
30 negative, the global value <<errno>> is set to <<EDOM>> (domain error).
34 <<sqrt>> is ANSI C. <<sqrtf>> is an extension.
37 /******************************************************************
41 * x - floating point value
47 * This routine performs floating point square root.
49 * The initial approximation is computed as
50 * y0 = 0.41731 + 0.59016 * f
51 * where f is a fraction such that x = f * 2^exp.
53 * Three Newton iterations in the form of Heron's formula
54 * are then performed to obtain the final value:
55 * y[i] = (y[i-1] + f / y[i-1]) / 2, i = 1, 2, 3.
57 *****************************************************************/
62 #ifndef _DOUBLE_IS_32BITS
70 /* Check for special values. */
85 return (z_infinity
.d
);
89 /* Initial checks are performed here. */
98 /* Find the exponent and mantissa for the form x = f * 2^exp. */
103 /* Get the initial approximation. */
104 y
= 0.41731 + 0.59016 * f
;
107 /* Calculate the remaining iterations. */
108 for (i
= 0; i
< 3; ++i
)
111 /* Calculate the final value. */
123 #endif /* _DOUBLE_IS_32BITS */