/* double ConvDecimalToDouble(x)
 *
 * ARGUMENT
 *	DEC	*x;
 *
 * DESCRIPTION
 *	Converts a DEC to a double.  It	requires a global variable eTwoTo32,
 *  2^32 in double notation, and ergGMPowersOfTenDouble, an array of
 *  powers of ten in double notation.
 *
 * SIDE	EFFECTS
 *	None.
 *
 * RETURNS
 *	The double if the conversion is	successful, and	the error code
 * otherwise.
 *
 * POSSIBLE ERROR CODES
 *
 *	GM_NULLPOINTER
 *
 * AUTHOR
 *  Jared Levy	Feb. 9,	1987
 *   Copyright (C) 1987-1990 Greenleaf Software	Inc.  All rights reserved.
 *
 * MODIFICATIONS
 *
 */

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

double	ConvDecimalToDouble(x)
DEC	*x;
{

	int	p;
	double	f, g;

	_MacStart(GM_DTODF);
	/* check for null pointer */
	_MacInVar(x, 0.0);

/* change 64-bit number	to double */
	f = (double) x->ls.lsl[0];
	if (f<0.0)
		f+= eTwoTo32;

	if (x->ls.lsl[1] != 0)	{
		g = (double) x->ls.lsl[1];
		if (g<0.0)
			g+=eTwoTo32;
	  f+= (g * eTwoTo32);
	}

	if (x->ls.lmsd != 0)
		f+= ((double) x->ls.lmsd) * eTwoTo64;

/* adjust by power of 10 */
	p = x->ls.lid;
	while (p>=25)  {
		f = f /	1e25;
		p-= 25;
	}
	f = f /	ergGMPowersOfTenDouble[p];

/* make	negative numbers negative */
	if (_MacIsDecN(x))  {
		f = -f;
	}

	_MacRet(f);
}