#include #include using namespace std; int main(int argc, char *argv[]) { long int max_num_term, i; double x, true_exp, approx_exp, new_approx_exp; double current_term, new_term; // set x and true value x = 1.5; true_exp = exp(x); // set max number of terms max_num_term = 6; // Calculation exp(x) = sum_{i=0} x^i/i! approx_exp = 1.0; current_term = 1.0; for(i = 1; i <= max_num_term; i++) { current_term *= x / (double)i; new_approx_exp = approx_exp + current_term; // don't add useless terms if(new_approx_exp == approx_exp) { cerr << "STOP!: i = " << i << endl; break; } approx_exp = new_approx_exp; } // print a result cout << "Approximation of exp(" << x << ") = " << approx_exp << endl; cout << "True value of exp(" << x << ") = " << true_exp << endl; return 0; }