/* Illustration of Horner's method */ #include #include #define NN 40 /* Check for a polynomial this size */ #define XVAL 0.50 /* Use this value */ double coeff[NN+7]; /* Polynomial coefficients */ /* Calculate */ /* f(x) = cof[0] + x*cof[1] + x^2 * cof[2] */ /* + ... + x^{n-1}*cof[n-1] */ /* */ /* (The polynomial has n coefficients and */ /* is of degree n-1) */ /* */ /* Horner's method can be written: */ /* nn=n; (The number of coefficients) */ /* fval=cof[--nn]; (Fetch the last coefficient) */ /* while (--nn>=0) (For each succeeding coefficient, */ /* fval=xx*fval + cof[nn]; multiply by xx and then add the coeff.) */ /* */ /* (See below for a worked-out example.) */ /* */ void main(void) { int i,nn; double fval, term; printf ("Compare Horner's method for evaluating a polynomial\n" " with an alternative method.\n"); printf ("Horner's method has N multiplications instead of 2N,\n" " and has slightly less roundoff error.\n"); printf ("\nUsing the %dth Taylor polynomial for -log(1-x) (x=%g)\n", NN, XVAL); /* Fill in the coefficient vector with NN entries */ /* corresponding to a Taylor polynomial for -log(1-x) */ coeff[0]=0.0; for (i=1; i=0) fval=XVAL*fval + coeff[nn]; /* The TRUE VALUE of -log(1-XVAL) = log(2) is stored in */ /* as the constant M_LN2 */ printf ("Polynomial value (Horner's method, n=%d): %12.10f\n", NN, fval); printf ("True value (n=infinity): %12.10f Difference: %g\n", M_LN2, M_LN2-fval); /* Compare with a plausible method that requires twice as many */ /* multiplications: */ fval=0.0; term=1.0; for (i=0; i=0) fval=xx*fval + cof[nn]; return fval; }