This is an example of a function that you could code for yourself.
Figure 1. SQRT EXEC
/* SQRT EXEC */
/* The SQUARE ROOT function. */
/* */
/* A function to calculate the square root of a number */
/* using the Newton-Raphson method. */
/* */
/* SQRT(number) */
/* */
/* where "number" is a nonnegative REXX number, */
/* returns the square root of "number". If the number */
/* is negative or not a decimal number, then this function will */
/* return a null character and report the error. */
arg num /* get the number */
null = ''
if ¬datatype(num,'Number') /* valid number? */
then do
say 'Invalid input argument:' Num'. Must be a positive decimal number.'
return null
end
if num < 0 /* check for negative */
then do
say 'Invalid input argument:' Num'. Must be a positive decimal number.'
return null
end
else if num = 0 then
return 0 /* check for 0 */
xnew = num /* initialize answer */
/* calculate maximum */
eps = 0.5 * 10**(1+fuzz()-digits()) /* accuracy */
/* Loop until a sufficiently accurate answer is obtained. */
do until abs(xold-xnew) < (eps*xnew)
xold = xnew /* save the old value */
xnew = 0.5 * (xold + num / xold) /* calculate the new */
end
xnew = xnew / 1 /* strip unnecessary zeros */
return xnew