// this program approximates the function e raised to the x power // using one, two, three, an four terms of an approximating // polynomial #include #include #include int main() { float x, funcvalue, approx, difference; cout << "\nEnter a value of x: "; cin >> x; // print two title lines cout << " e to the x Approximation Difference\n"; cout << "------------- ------------- -------------\n"; funcValue = exp(x); // use the library function // calculate the first approximation approx = 1; difference = fabs(funcvalue - approx); cout << setw(10) << setiosflags(ios::showpoint) << funcvalue << setw(18) << approx << setw(18) << difference << endl; // calculate the second approximation approx = approx + x; difference = fabs(funcValue - approx); cout << setw(10) << setiosflags(ios::showpoint) << funcValue << setw(18) << approx << setw(18) << difference << endl; // calculate the third approximation approx = approx + pow(x,2)/2.0; difference = fabs(funcvalue - approx); cout << setw(10) << setiosflags(ios::showpoint) << funcvalue << setw(18) << approx << setw(18) << difference << endl; // calculate the fourth approximation approx = approx + pow(x,3)/6.0; difference = fabs(funcValue - approx); cout << setw(10) << setiosflags(ios::showpoint) << funcValue << setw(18) << approx << setw(18) << difference << endl; return 0; }