'use strict';

// MODULES //

var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );


// MAIN //

/**
* Evaluates a rational function (i.e., the ratio of two polynomials described by the coefficients stored in \\(P\\) and \\(Q\\)).
*
* ## Notes
*
* -   Coefficients should be sorted in ascending degree.
* -   The implementation uses [Horner's rule][horners-method] for efficient computation.
*
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
*
* @private
* @param {number} x - value at which to evaluate the rational function
* @returns {number} evaluated rational function
*/
function evalrational( x ) {
	var ax;
	var s1;
	var s2;
	if ( x === 0.0 ) {
		return {{ratio}};
	}
	if ( x < 0.0 ) {
		ax = -x;
	} else {
		ax = x;
	}
	if ( ax <= 1.0 ) {
		s1 = {{P_ASCENDING}};{{P_ASCENDING_ESLINT}}
		s2 = {{Q_ASCENDING}};{{Q_ASCENDING_ESLINT}}
	} else {
		x = float64ToFloat32( 1.0 / x );
		s1 = {{P_DESCENDING}};{{P_DESCENDING_ESLINT}}
		s2 = {{Q_DESCENDING}};{{Q_DESCENDING_ESLINT}}
	}
	return float64ToFloat32( s1 / s2 );
}


// EXPORTS //

module.exports = evalrational;
