public double evaluate ( double variable ) { if ( size == 0 ) { return 0 ; } else if ( size == 1 ) { return c [ 0 ] ; } else if ( Double . isInfinite ( variable ) ) { // Only the largest power with a non-zero coefficient needs to be evaluated int degree = computeDegree ( ) ; if ( degree % 2 == 0 ) variable = Double . POSITIVE_INFINITY ; if ( c [ degree ] < 0 ) variable = variable == Double . POSITIVE_INFINITY ? Double . NEGATIVE_INFINITY : Double . POSITIVE_INFINITY ; return variable ; } // Evaluate using Horner's Method. // This formulation produces more accurate results than the straight forward one and has fewer // multiplications double total = c [ size - 1 ] ; for ( int i = size - 2 ; i >= 0 ; i -- ) { total = c [ i ] + total * variable ; } return total ; }
Computes the polynomials output given the variable value Can handle infinite numbers