Numerical Computing with IEEE Floating Point Arithmetic

Computing the Exponential Function without a Math Library

For our second example illustrating stability and instability, let us attempt to compute the exponential function exp( x) directly, without any calls to library functions. From (12.7), we know that exp is a well-conditioned function as long as x is not too large. We use the well-known Taylor series


This allows us to approximately compute the limiting sum by means of a simple loop, noting that successive terms are related by


Thus, each term is easily computed from the previous term by multiplying by x and dividing by n. How should we terminate the loop? The simplest way would be to continue until the new term in the sum underflows to zero, as in Program 2 (Chapter 10). A better solution is to use the idea in Program 3 (Chapter 10): the loop may be terminated when the new term is small enough that adding it to the previous terms does not change the sum. Program 9 implements this idea using single precision.

#include main()  /* Program 9: Compute exp(x) from its Taylor series */{int n;float x, term, oldsum, newsum;printf("Enter x \n");scanf("%e", &x);n=0;oldsum = 0.0;newsum = 1.0;term = 1.0;/* terminates when the new sum is no different from the old sum */while (newsum !=oldsum){oldsum = newsum;n++;term = term*x/n; /* term has the value (x^n)/(n!) */newsum = newsum + term; /* approximates exp(x) */printf("n = %3d term = %13.6e newsum = %13.6e \n",n,term,newsum);}printf("From summing the series,...

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Radiation Detectors
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.