campo-sirio/gfm/dfact.c

62 lines
880 B
C
Raw Normal View History

/* DEC *Factorial(pDst, n)
*
* ARGUMENT
* DEC *pDst;
* unsigned n;
*
* DESCRIPTION
* Sets pDst = n!
*
* SIDE EFFECTS
* None.
*
* RETURNS
* pDst if successful, GM_NULL otherwise
*
* POSSIBLE ERRORS
* GM_NOMEMORY
* GM_OVERFLOW (if n > 20)
*
* AUTHOR
* Jared Levy
* Copyright (C) 1987-1990 Greenleaf Software Inc. All rights reserved.
*
* MODIFICATIONS
*
*
*/
#include <stdio.h>
#include "gm.h"
#include "gmsystem.h"
DEC *Factorial(pDst, n)
DEC *pDst;
unsigned n;
{
DEC dmult, *mult = &dmult;
unsigned i;
_MacStart(GM_DFACT);
_MacOutVarD(pDst);
/* check for overflow */
if (n > 20) {
_MacErr(GM_OVERFLOW);
_MacRet(GM_NULL);
}
/* do calculations */
_MacDCopy(pDst, &decOne);
_MacDCopy(mult, &decOne);
for (i=2; i<=n; i++) {
mult->dc.sl[0] = i;
_MulDec80Bit(pDst, pDst, mult);
}
/* pDst fits into 64 bits */
_MacRet(pDst);
}