loop.js.txt (676B)
1 'use strict'; 2 3 // VARIABLES // 4 5 var C = [ 6 {{coefficients}} 7 ]; 8 var END = C.length - 1; 9 10 11 // MAIN // 12 13 /** 14 * Evaluates a polynomial. 15 * 16 * ## Notes 17 * 18 * - The implementation uses [Horner's rule][horners-method] for efficient computation. 19 * 20 * [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method 21 * 22 * 23 * @private 24 * @param {number} x - value at which to evaluate the polynomial 25 * @returns {number} evaluated polynomial 26 */ 27 function evalpoly( x ) { 28 var p; 29 var i; 30 31 if ( x === 0.0 ) { 32 return C[ 0 ]; 33 } 34 i = END; 35 p = ( C[ i ] * x ) + C[ i-1 ]; 36 i -= 2; 37 while ( i >= 0 ) { 38 p = ( p * x ) + C[ i ]; 39 i -= 1; 40 } 41 return p; 42 } 43 44 45 // EXPORTS // 46 47 module.exports = evalpoly;