home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / RXCALC.ZIP / PROGRAM2.CAL < prev    next >
Text File  |  1991-05-15  |  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.