home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / viscobv6.zip / vac22os2 / ibmcobol / samples / toolkit / rexx / api / rexxcalc / program2.cal < prev    next >
Text File  |  1996-11-19  |  1KB  |  27 lines

  1. /* Rexx Calculator program 2 key - square root */
  2. parse arg num,  right                  /* get the two operands       */
  3. numeric digits CalcPrecision()         /* set current precision      */
  4. numeric form value CalcForm()          /* set current form           */
  5.  
  6. /* Function to calculate the square root of a number using           */
  7. /* the Newton-Raphson method                                         */
  8.  
  9. if num < 0                             /* check for negative         */
  10.   then return 'Error'
  11.   else if num = 0 then return 0        /* check for 0                */
  12.  
  13. xnew = num                             /* initialize answer          */
  14.  
  15.                                        /* calculate maximum          */
  16. eps = 0.5 * 10**(1+fuzz()-digits())    /* accuracy                   */
  17.  
  18. /* Loop until a sufficiently accurate answer is obtained.            */
  19.  
  20. do until abs(xold-xnew) < (eps*xnew)
  21.   xold = xnew                          /* save the old value         */
  22.   xnew = 0.5 * (xold + num / xold)     /* calculate the new          */
  23. end
  24. xnew = xnew / 1                        /* make it pretty             */
  25.  
  26. return xnew
  27.