
////////////////  Math - PASCAL routines converted ... ////////////////////

function sqr ( arg )
{
	return arg*arg;
}

function RadToDeg ( arg )
{
	return (arg/(2.0*m_PI)*360.0);
}

function DegToRad ( arg )
{
	return (arg/360.0*(2.0*m_PI));
}

function Fmod2p ( arg )
{
	var modu, ret;
	var twopi = 2.0*m_PI;
	modu = arg - Math.floor(arg/twopi) * twopi;
	if (modu >= 0.0)
		ret = modu;
	else
		ret = modu + twopi;
	return ret;
}

function Modulus( arg1, arg2 )
{
	var modu;
	modu = arg1 - Math.floor(arg1/arg2) * arg2;
//alert ("modu=" + modu + " arg1=" + arg1 + " arg2=" + arg2);
	if (modu >= 0.0)	return modu;
	else			return modu + arg2;
}

function AcTan( sinx, cosx )
{
	// The AcTan - bug. Here some e-mail excerpts ...

// The "AcTan bug" was introduced in Dr. Kelso's translation of the
// original FORTRAN code to his Pascal version. The code has a
// two-argument arctangent function, which returns a value from 0( to
// 360( in the FORTRAN version, but goes from -90( to 270( in the
// Pascal version. It may have been that this change was made so that
// AcTan could be used when determining latitude, and because some
// test cases may have shown absolutely no effect from the change.
// Indeed, sgp4 results appear to not be affected at all by this
// change. And some sdp4 cases are also not affected (probably because
// the calls to AcTan were not in the fourth quadrant, which is the
// only place that there is a difference). However, sdp4 results can
// be affected ...

// But this ACTAN function is completely unnecessary. I never coded it. As you 
// pointed out, every decent programming language has a 2-argument arctangent 
// function that is quadrant-preserving. For my FORTRAN, it's ATAN2(Y,X), where 
// Y is the sine of the angle, and X is the cosine. It returns values from -pi 
// to +pi. A simple change will return values from 0 to 2*pi: 
// Angle = Pi - ATAN2(-Y,-X) 

// atan2 definnition in C++ :
// atan2 returns a value in the range -pi to +pi radians, 
// using the signs of both parameters to determine the quadrant 
// of the return value. 
	var ret;

	if ( (sinx == 0.0) && (cosx == 0.0) )
		ret = 270.0;
	else	{
		ret = Math.atan2(sinx, cosx);
		if (ret <= -m_PI/2.000000001)	// This will adapt this version with the 'ugly' 
			ret += 2.0*m_PI;			// one below. Both functions return exactly the same values
	}
// the old version self made ...
/*
	if (cosx == 0.0)	{
		if (sinx > 0.0)
			ret = PI/2.0;
		else
			ret = 3.0*PI/2.0;
	}
	else if (cosx > 0.0) {
	// --------- correction to match FORTRAN version ----------}
//		if (sinx > 0)						// Add
			ret = atan(sinx/cosx);
//		else								// Add
//			ret = 2*PI + atan(sinx/cosx);	// Add
	// --------------------------------------------------------}
 	}
	else
		ret = PI + atan(sinx/cosx);
	}
*/
	return ret;

}

function round ( arg )
{
	return Math.round (arg);
//	double fFrac, fInt;
//	fFrac = modf(arg, &fInt);
//	if (fFrac >= 0.5) fInt ++;
//	return (long) fInt;
}




