SQRT routine
[Autolink] Menu
/* ------------------------------------------------------------------ */
/* Using the Newton-Raphson method for finding roots of equations */
/* gives the procedure below for finding the square root of N */
/* */
/* (found in a public news group) */
/**********************************************************************/
numeric digits 21
do forever
call CharOut, "Enter a number (RETURN to exit): "
thisNumber = strip( lineIN() )
select
when thisNumber = "" then
leave
when datatype( thisNumber ) <> "NUM" then
say "Invalid response!"
when thisNumber <= 0 then
say "Invalid number!"
otherwise
do
thisSqrt = SquareRoot( thisNumber )
Say " The Square root of " || thisNumber || " is " || thisSqrt
Say " Controlvalue: "
Say " " || thisSqrt "*" thisSqrt "=" thisSqrt*thisSqrt
end /* otherwise */
end /* select */
end /* do forever */
exit 0
/* ------------------------------------------------------------------ */
/* function: calculate the square root of a number */
/* */
/* call: SquareRoot number */
/* */
/* where: number - number to calculate the square root of */
/* */
/* returns: the square root */
/* */
SquareRoot:
parse arg n
x1=1
x0=N
if N<>0 then
do
/* do until x1=x0 */
do until abs(x0-x1) < 0.00001
x0=x1
x1=((x0*x0)+N)/(2*x0)
end /* do until */
end /* if */
else
x1=0
return x1
[Back: Mathematical routines]
[Next: Sqrt routine]