/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include <math.h>
#include <libm.h>

/****************************************************************************/
/*					Local definitions										*/
/****************************************************************************/

#define	PI4		0.7853981633974483096282022398515465511082

/****************************************************************************/
/*					Local data												*/
/****************************************************************************/

/*
*	Polynom coefficients for the Chebyshev approximation.
*/

static const float poly[] = {

	+1.000000000000e+00,
	-3.333327629198e-01,
	+1.999807528115e-01,
	-1.426001608282e-01,
	+1.093234150130e-01,
	-8.349724968308e-02,
	+5.708955593045e-02,
	-3.035186478592e-02,
	+1.048764925502e-02,
	-1.701170064084e-03
};

/****************************************************************************/
/*					Public API functions									*/
/****************************************************************************/

/** @addtogroup func */
/** @{ */

/**
*	Arcus tangent function.
*
*	@param	x		The value of which the arcus tangent should be calculated
*	@retval	NaN		The argument was NaN
*	@retval	+PI/2	The argument was positive infinity
*	@retval	-PI/2	The argument was negative infinity
*	@retval	atan(x)	Otherwise. The returned value is in radian and in the
*					principal range of [-PI/2,+PI/2].
*/

float	atanf( float x )
{
	if ( isnan( x ) ) return( x );
	if ( isinf( x ) > 0 ) return( +PI4 * 2 );
	if ( isinf( x ) < 0 ) return( -PI4 * 2 );

	if ( x > 1.0 ) {

		x = 1 / x;
		return( x * polynomf( x * x, poly, 9 ) + PI4 );
	}
	else if ( x < 1.0 ) {

		x = 1 / x;
		return( x * polynomf( x * x, poly, 9 ) - PI4 );
	}
	else {

		return( x * polynomf( x * x, poly, 9 ) );
	}
}

/** @} */

