The Taylors series for the exponential function is given by

The Taylor\'s series for the exponential function is given by Write a C program to a) Write a program to find e 10 with the Taylor\'s series in single precision, and b) Write a program to find e 10 with a Taylor\'s series in double precision. c) Can you explain the difference in results between (a) and (b)? n these programs do not test for convergence. Just use an infinite while loop and monitor the output as each new term is added. When the result converges, terminate the program with control-c. Notes: 1) e 10 539 992 976 248 485 15X10 2) Remember that single-precision literals need an \"f or \"F\" as a suffix. 3) Make sure the single precision calculations are completely in single precision and the double precision completely in double precision or else you will not get the correct results. 4) For the factorial function, use the C function you wrote for Lab Assignment 2.

Solution

C++ code for single precision:

#include<bits/stdc++.h>
using namespace std;

float factorial1(float n)
{
float f = 1.0;
while(n > 1.0)
{
    f = f * n;
    n = n - 1.0f;
}
return f;
}

int main()
{

// Single Precision Calculation
   cout << \"Single Precision Calculation\" << endl;
   float ans = 1.0f;
   float n = 1.0f;
   float x = -10.0f;
while(n <= 37.0f)
{
   ans = ans + pow(x,n)/factorial1(n) ;
   n = n + 1.0f;
   cout << \"After adding term \" << n << \" serise sum = \" << ans << endl;
}
   return 0;
}

C++ code for Double precision:

#include<bits/stdc++.h>
using namespace std;

double factorial2(double n)
{
double f = 1.0;
while(n > 1.0)
{
    f = f * n;
    n = n - 1.0;
}
return f;
}


int main()
{

   cout << \"Double Precision Calculation\" << endl;
// Double Precision Calculation
   double ans2 = 1.0;
   double n2 = 1.0;
   double x2 = -10.0;
while(n2 <= 45.0)
{
   ans2 = ans2 + pow(x2,n2)/factorial2(n2) ;
   n2 = n2 + 1.0;
   cout << \"After adding term \" << n2 << \" serise sum = \" << ans2 << endl;
}


   return 0;
}

Note:

In single precision, the result conserges after adding 34 terms.

In single precision, the value of e^-10 = 0.000480763.

In Double precision, the result conserges after adding 42 terms.

In Double precision, the value of e^-10 = 4.53999e-05

 The Taylor\'s series for the exponential function is given by Write a C program to a) Write a program to find e 10 with the Taylor\'s series in single precisio
 The Taylor\'s series for the exponential function is given by Write a C program to a) Write a program to find e 10 with the Taylor\'s series in single precisio

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site