/* DEC	*ConvNominalToEffective(eff, nom, nper)
 *
 * ARGUMENT
 *	DEC	*eff,*nom;
 *	int	nper;
 *
 * DESCRIPTION
 *	Changes	a nominal interest rate	(the stated percentage before
 *  compounding	is considered) to an effective interest	rate due to the
 *  effects of compound	interest.  nom is the nominal interest rate and
 *  nper is the	number of compounding periods.	The resulting effective
 *  rate is stored in eff.
 *	nper is	the number of compounding periods, and is GM_CONTINUOUS
 *  for	continuous compounding.
 *	finite compounding:  eff = (1 +	nom / nper) ^ nper
 *	continuous compounding:	 eff = e^nom - 1
 *
 * SIDE	EFFECTS
 *	eff is set to the effective percentage interest	rate
 *
 * RETURNS
 *	eff if successful, otherwise GM_NULL
 *
 * POSSIBLE ERRORS
 *	GM_NULLPOINTER
 *	GM_ARGVAL
 *
 * AUTHOR
 *  Jared Levy
 *   Copyright (C) 1988-1990 Greenleaf Software	Inc.  All rights reserved.
 *
 * MODIFICATIONS
 *
 *
 */

#include <stdio.h>
#include "gmsystem.h"

DEC	*ConvNominalToEffective(eff, nom, nper)
DEC	*eff,*nom;
int	nper;
{
	DEC	dtemp, *temp=&dtemp, ddper, *dper=&ddper;

	_MacStart(GM_NOMTOEFF);

	_MacInVarD(nom);
	_MacOutVarD(eff);

/* nper	must be	>= 1 if	not continuous compounding */
	if (((nper<=0)&&(nper!=GM_CONTINUOUS))
		||CompareDecimal(nom,&decMinusHundred)<=0)  {
		_MacErr(GM_ARGVAL);
		_MacRet(GM_NULL);
	}

/* convert percent to decimal */
	_MacDCopy(temp,	nom);
	temp->dc.id+=2;

	if (nper==GM_CONTINUOUS)  {	/* continuous compounding */
		if (_ExpDec80Bit(temp, temp)!=GM_SUCCESS)  {
			_MacErr(GM_ARGVAL);
			_MacRet(GM_NULL);
		}

		(void) _SubDec80Bit(temp, temp,	&decOne);
	}
	else  {				/* finite compounding */
		(void) ConvLongToDecimal(dper, (long) nper);
		(void) _DivDec80Bit(temp, temp,	dper);
		(void) _AddDec80Bit(temp, temp,	&decOne);

		if (_IntPwrDec80Bit(temp, temp,	nper)!=GM_SUCCESS)  {
			_MacErr(GM_ARGVAL);
			_MacRet(GM_NULL);
		}

		(void) _SubDec80Bit(temp, temp,	&decOne);
	}

/* convert decimal to percent */
	if (temp->dc.id>=2)
		temp->dc.id-=2;
	else  {
		(void) ConvLongToDecimal(dper, 100L);
		(void) _MulDec80Bit(temp, temp,	dper);
	}

	if (_Sq5UnsTo4Uns(temp)!=GM_SUCCESS)  {
		_MacErr(GM_ARGVAL);
		_MacRet(GM_NULL);
	}

	_MacDCopy(eff,temp);
	_MacRet(eff);
}