/* DEC	*ConvEffectiveToNominal(nom, eff, nper)
 *
 * ARGUMENT
 *	DEC	*eff,*nom;
 *	int	nper;
 *
 * DESCRIPTION
 *	Changes	to a nominal interest rate (the	stated percentage before
 *  compounding	is considered) from an effective interest rate due to the
 *  effects of compound	interest.  eff is the effective	interest rate and
 *  nper is the	number of compounding periods.	The resulting nominal
 *  rate is stored in nom.
 *	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
 *	nom is set to the effective percentage interest	rate
 *
 * RETURNS
 *	nom if successful, otherwise GM_NULL
 *
 * POSSIBLE ERRORS
 *	GM_NULLPOINTER
 *	GM_ARGVAL
 *
 * AUTHOR
 *  Jared Levy
 *   Copyright (C) 1989-1990 Greenleaf Software	Inc.  All rights reserved.
 *
 * MODIFICATIONS
 *
 *
 */

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

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

	_MacStart(GM_EFFTONOM);

	_MacInVarD(eff);
	_MacOutVarD(nom);

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

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

	if (nper==GM_CONTINUOUS)  {	/* continuous compounding */
		(void) _AddDec80Bit(temp, temp,	&decOne);
		(void) _LnDec80Bit(temp, temp);
	}
	else  {				/* finite compounding */
		(void) ConvLongToDecimal(dper, (long) nper);
		(void) _AddDec80Bit(temp, temp,	&decOne);
		(void) _LnDec80Bit(temp, temp);
		(void) _DivDec80Bit(temp, temp,	dper);
		(void) _ExpDec80Bit(temp, temp);
		(void) _SubDec80Bit(temp, temp,	&decOne);
		(void) _MulDec80Bit(temp, temp,	dper);
	}

/* 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(nom,temp);
	_MacRet(nom);
}