2.4 Programming Examples on Loss of Precision and Round-offErrors 25
- 0 E− 10 , so that for a certain value ofx> 0 , there is always a value ofn=Nfor which the
loss of precision in terminating the series atn=Nis always smaller than the next term in the
seriesx
N
N!. The latter is implemented through the while{...} statement.
http://folk.uio.no/mhjensen/compphys/programs/chapter02/cpp/program4.cpp
// Program to calculate function exp(-x)
// using straightforward summation with differing precision
using namespacestd;
#include
// type float: 32 bits precision
// type double: 64 bits precision
#defineTYPE double
#definePHASE(a) (1 - 2*(abs(a) % 2))
#defineTRUNCATION 1.0E-10
// function declaration
TYPE factorial(int);
intmain()
{
int n;
TYPE x, term, sum;
for(x = 0.0; x < 100.0; x += 10.0){
sum = 0.0; //initialization
n = 0;
term = 1;
while(fabs(term) > TRUNCATION){
term = PHASE(n)*(TYPE) pow((TYPE) x,(TYPE) n) / factorial(n);
sum += term;
n++;
}// end of while() loop
cout << x =''<< x <<
exp = << exp(-x) <<
series = << sum; cout <<
number of terms =" << n << endl;
}// end of for() loop
return 0;
}// End: function main()
// The function factorial()
// calculates and returns n!
TYPE factorial(int n)
{
int loop;
TYPE fac;
for(loop = 1, fac = 1.0; loop <= n; loop++){
fac*= loop;
}
return fac;
}// End: function factorial()
There are several features to be noted^3. First, for low values ofx, the agreement is good,
however for largerxvalues, we see a significant loss of precision. Secondly, forx= 70 we
have an overflow problem, represented (from this specific compiler) by NaN (not a number).
The latter is easy to understand, since the calculation of a factorial of the size171!is beyond
the limit set for the double precision variable factorial. The message NaN appears since the
computer sets the factorial of 171 equal to zero and we end up having a division by zero in
our expression fore−x.
(^3) Note that different compilers may give different messages and deal with overflow problems in different
ways.