home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
xbase
/
library
/
clipper
/
rettig
/
source
/
fact.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-10-21
|
1KB
|
45 lines
/*********
*
* FACT.C
*
* by Ralph Davis
*
* Placed in the public domain by Tom Rettig Associates, 10/22/1990.
*
* Syntax: FACT( <expN> )
* Return: <expn1> factorial if <expN> <= 18
* (above 18 precision is lost)
* otherwise _tr_infinity()
*********/
#include "trlib.h"
TRTYPE fact()
{
int i;
double ret;
if ( PCOUNT == 1 && ISNUM(1) )
{
i = _parni(1);
if (i > 18)
_retnd(_tr_infinity()); /* If number is greater than 18,
precision disappears, so
return infinity as error signal */
else if (i <= 0) /* If number is 0 or negative,
return 0 */
_retnd( 0.0 );
else
{
ret = 1.0;
for (; i > 1; i--) /* This loop computes the product
of all the numbers from i to 1. */
ret *= (double) i;
_retnd( ret );
}
}
else
_retnd( ERRORNEG ); /* if error return -1 */
}