home *** CD-ROM | disk | FTP | other *** search
- /*********
- *
- * ROOT.C
- *
- * by Ralph Davis
- *
- * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
- *
- * Syntax: ROOT( <expN1>, <expN2> )
- * Return: The <expN2>th root of <expN1>
- * If <expN2> is even and <expN1> is negative,
- * returns INFINITY().
- *********/
-
- #include "trlib.h"
-
- TRTYPE root()
- {
- double n1, n2, ret;
- int minus;
-
- if ( PCOUNT == 2 && ISNUM(1) && ISNUM(2) )
- {
- n1 = _parnd(1);
- n2 = _parnd(2);
-
- if ( n1 < 0.0 )
- {
-
- /* We can't take the even root of a negative number */
-
- if ( _tr_fmod(n2, 2.0) == 0.0)
- {
- _retnd(_tr_infinity());
- return;
- }
-
- /* If number is negative and root is odd, take absolute
- value and set flag */
-
- n1 = -n1;
- minus = TRUE;
- }
- else
- minus = FALSE;
-
- /* The nth root of a number = the number to the 1/nth power */
-
- ret = ( minus ? -(_tr_pow(n1, (1.0 / n2))) : (_tr_pow(n1, (1.0 / n2))) );
-
- _retnd( ret );
- }
- else
- _retnd(ERRORNEG); /* error -1 value */
- }
-