home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / rettig.zip / TRSOURCE.EXE / ROOT.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  1KB  |  56 lines

  1. /*********
  2. *
  3. * ROOT.C
  4. *
  5. * by Ralph Davis
  6. *
  7. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  8. *
  9. *  Syntax:  ROOT( <expN1>, <expN2> )
  10. *  Return:  The <expN2>th root of <expN1>
  11. *           If <expN2> is even and <expN1> is negative,
  12. *              returns INFINITY().
  13. *********/
  14.  
  15. #include "trlib.h"
  16.  
  17. TRTYPE root()
  18. {
  19.    double n1, n2, ret;
  20.    int minus;
  21.  
  22.    if ( PCOUNT == 2 && ISNUM(1) && ISNUM(2) )
  23.    {
  24.       n1 = _parnd(1);
  25.       n2 = _parnd(2);
  26.  
  27.       if ( n1 < 0.0 )
  28.       {
  29.  
  30.          /* We can't take the even root of a negative number */
  31.  
  32.          if ( _tr_fmod(n2, 2.0) == 0.0)
  33.          {
  34.             _retnd(_tr_infinity());
  35.             return;
  36.          }
  37.  
  38.          /* If number is negative and root is odd, take absolute
  39.             value and set flag */
  40.  
  41.          n1 = -n1;
  42.          minus = TRUE;
  43.       }
  44.       else
  45.          minus = FALSE;
  46.  
  47.       /* The nth root of a number = the number to the 1/nth power */
  48.  
  49.       ret = ( minus ? -(_tr_pow(n1, (1.0 / n2))) : (_tr_pow(n1, (1.0 / n2))) );
  50.  
  51.       _retnd( ret );
  52.    }
  53.    else
  54.       _retnd(ERRORNEG);   /* error -1 value */
  55. }
  56.