evalrational.js.txt (982B)
1 'use strict'; 2 3 // MAIN // 4 5 /** 6 * Evaluates a rational function, i.e., the ratio of two polynomials described by the coefficients stored in \\(P\\) and \\(Q\\). 7 * 8 * ## Notes 9 * 10 * - Coefficients should be sorted in ascending degree. 11 * - The implementation uses [Horner's rule][horners-method] for efficient computation. 12 * 13 * [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method 14 * 15 * 16 * @private 17 * @param {number} x - value at which to evaluate the rational function 18 * @returns {number} evaluated rational function 19 */ 20 function evalrational( x ) { 21 var ax; 22 var s1; 23 var s2; 24 if ( x === 0.0 ) { 25 return {{ratio}}; 26 } 27 if ( x < 0.0 ) { 28 ax = -x; 29 } else { 30 ax = x; 31 } 32 if ( ax <= 1.0 ) { 33 s1 = {{P_ASCENDING}};{{P_ASCENDING_ESLINT}} 34 s2 = {{Q_ASCENDING}};{{Q_ASCENDING_ESLINT}} 35 } else { 36 x = 1.0 / x; 37 s1 = {{P_DESCENDING}};{{P_DESCENDING_ESLINT}} 38 s2 = {{Q_DESCENDING}};{{Q_DESCENDING_ESLINT}} 39 } 40 return s1 / s2; 41 } 42 43 44 // EXPORTS // 45 46 module.exports = evalrational;