Horner.java


Below is the syntax highlighted version of Horner.java from §2.1 Static Methods.


/******************************************************************************
 *  Compilation:  javac Horner.java
 *  Execution:    java Horner
 *
 *  A static method for evaluating a polynomial using Horner's method.
 *
 *  The test client takes a command-line argument n, computes n terms
 *  of the Taylor series e^x = 1 + x + x^2/2! + ..., and reads in
 *  values x from standard input, and prints out the polynomial
 *  evaluates at x (and compares it against Math.exp(x)).
 *
 *  % java java Horner 30
 *  0
 *  1.0
 *  1.0
 *
 *  1
 *  2.718281828459045
 *  2.7182818284590455
 *
 *  2
 *  7.38905609893065
 *  7.38905609893065
 *
 *  .5
 *  1.6487212707001282
 *  1.6487212707001282
 *
 *  -.1
 *  0.9048374180359595
 *  0.9048374180359595
 *
 ******************************************************************************/

public class Horner {

    // use Horner's method to compute and return the polynomial evaluated at x
    // p[0] + p[1] x^1 + p[2] x^2 + ... + p[n-1] x^n-1
    public static double eval(double x, double[] p) {
        double result = 0;
        for (int i = p.length - 1; i >= 0; i--)
            result = p[i] + (x * result);
        return result;
    }

    public static void main(String[] args) {
        int n  = Integer.parseInt(args[0]);

        // compute coeffients for Taylor series e^x = 1 + x + x^2/2! + x^3/3! + ...
        double[] p = new double[n];
        p[0] = 1;
        for (int i = 1; i < n; i++) {
            p[i] = p[i-1] / i;
        }

        // evaluate the polynomial at values x read from standard input
        while (!StdIn.isEmpty()) {
            double x = StdIn.readDouble();
            StdOut.println(eval(x, p));
            StdOut.println(Math.exp(x));
            StdOut.println();
        }
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:15:13 EDT 2022.