Implementing exponentials.

exercise No. 117

The exponential f x = e x

Q:

The exponential f x = e x is being defined by a power series:

Equation 1. Power series definition of f x = e x
e x = 1 + x 1 1 ! + x 2 2 ! + x 3 3 ! + ... = i = 0 x i i !

Implement a class method double exp(double) inside a class Math choosing a package of your choice. The name clash with the Java standard class java.lang.Math is intended: You'll learn how to resolve naming conflicts.

Regarding practical calculations we replace the above infinite series by a limited one. So the number of terms to be considered will become a parameter which shall be configurable. Continue the implementation of the following skeleton:

Figure 297. An implementation sketch for the exponential
public class Math {
 ...

  /**
   * @param seriesLimit The last term's index of a power series to be included,
   */
  public static void setSeriesLimit(int seriesLimit) {...}

  /**
   * Approximating the natural exponential function by a finite
   * number of terms from the corresponding power series.
   *
   * A power series implementation has to be finite since an
   * infinite number of terms requires infinite execution time.
   *
   * The number of terms to be considered can be set by {@link #setSeriesLimit(int)}}
   *
   * @param x
   * @return
   */
  public static double exp(double x) {...}
}

Compare your results using seriesLimit=8 terms and the corresponding values from the professional implementation java.lang.Math.exp by calculating e -3 , e -2 , e 1 , e 2 and e 3 . What do you observe? Can you explain this result?

Do not forget to provide suitable Javadoc comments and check the generated HTML documentation for correctness.

Hints:

  • You should only use basic arithmetic operations like +, - * and /. Do not use Math.pow(double a, double b) and friends! Their implementations use power series expansions as well and are designed for a different purpose like having fractional exponent values.

  • The power series' elements may be obtained in a recursive fashion like e.g.:

    x 3 3 ! = x 3 x 2 2 !

A:

Regarding the finite number of terms we provide a class variable ❶ having default value of 5 corresponding to just the first 1 + 5 = 6 terms:

e x 1 + x 1 1 ! + ... + x 5 5 !

We also provide a corresponding setter method ❷ enabling users of our class to choose a different value:

public class Math {

  static int seriesLimit = 5; ❶

  /**
   *
   * @param seriesLimit The last term's index of a power series to be included,
   *   {@link }}.
   */
  public static void setSeriesLimit(int seriesLimit) { ❷
    Math.seriesLimit = seriesLimit;
  } ...

Calculating values by a finite series requires a loop:

public static double exp(double x) {
  double currentTerm = 1.,  // the first (i == 0) term x^0/0!
         sum = currentTerm;    // initialize to the power series' first term

  for (int i = 1; i <= seriesLimit; i++) {  // i = 0 has already been completed.
    currentTerm *= x / i;
    sum += currentTerm;
  }
  return sum;
}

You may also view the Javadoc and the implementation of double Math.exp(double). We may use the subsequent code snippet for testing and comparing our implementation:

Math.setSeriesLimit(6);

double byPowerSeries = Math.exp(1.);
System.out.println("e^1=" + byPowerSeries + ", difference=" +
                 (byPowerSeries - java.lang.Math.exp(1.)));

byPowerSeries = Math.exp(2.);
System.out.println("e^2=" + byPowerSeries + ", difference=" +
                 (byPowerSeries - java.lang.Math.exp(2.)));

byPowerSeries = Math.exp(3.);
System.out.println("e^3=" + byPowerSeries + ", difference=" +
                 (byPowerSeries - java.lang.Math.exp(3.)));

In comparison with a professional implementation we have the following results:

e^1=2.7180555555555554, difference=-2.262729034896438E-4
e^2=7.355555555555555, difference=-0.033500543375095226
e^3=19.412499999999998, difference=-0.67303692318767

Our implementation based on just 6 terms is quite good for small values of x. Larger values however exhibit growing differences.

This is due to the fact that our approximation is in fact just a polynomial of degree 6:

Figure 298. Comparing exponential and approximation
e x and p 6 x = 1 + x + x 2 2! + x 3 3! + x 4 4! + x 5 5! + x 6 6!
Comparing exponential and approximation

The plots show a particularly bad approximation for negative values. But for larger values outside the above scope the exponential will quickly diverge from its polynomial approximation as well.