Hi, I've just started a course in C++ about 3 weeks ago. Unfortunately, I'm pretty lost at the moment with this problem and my teacher said I'm on my own now. (I'm guessing he doesn't want to answer my questions anymore because I ask too many, but I really don't understand).
Anyways, as my last resort I've decided to join this forum and ask here because I've got lots of help from old threads from this forum.
The situation is a factorial problem:
I need to create a program that calculates e^x = 1 + x/1! + x^2/2! + ... and so forth.
I input the value for x, and the program should keep calculating until it reaches a number less than 1.0E-20 and terminates because the value is too small to make a different to the answer. Then the program returns how many terms it used to get to the result. (I'm guessing th it means the "accuracy" or iterations it took to come to this conclusion. I need to do this using a while loop.
So far my code looks llike this:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
#include <iomanip>
using std::setw;
using std::setprecision;
using std::setiosflags;
// factorial calculation (e = 1/1! + 1/2! + ...)
long long factorial( int num )
{
long long answer = 1;
while ( num > 1 ) {
answer *= num;
num--;
}
return answer;
}
// start of function main
int main()
{
// variable declarations
double x = -1;
// first while loop
while ( x < 1 || x > 156 ) {
cout << "Input a value for x between 1 and 156: ";
cin >> x;
}
int iterations = 0;
double answer = 1;
double result = 1;
int currentFactor = 1;
long double pow_x = 1;
while ( result > 1.0E-20) {
pow_x *= x;
result = pow_x / factorial ( currentFactor );
cout << result << endl; // I use this here to see the results of the factorial
currentFactor++;
answer += result;
iterations++;
}
// print results
cout << setiosflags ( ios::fixed | ios::showpoint );
cout << "\n" << "e raised to the " << x << " power is " << answer << endl;
cout << "\nThe number of terms computed is " << iterations << "\n" << endl;
system("pause");
return 0; // indicate successful termination
} // end of function main
Thats the code so far. The answer doesn't seem correct because when I print the result to the terminal, weird negative numbers show up. See below:
---
Input a value for x between 1 and 156: 1
1
0.5
0.16666666666666666
0.041666666666666664
0.0083333333333333332
0.0013888888888888889
0.00019841269841269841
2.4801587301587302e-005 // everything from here down is in scientific notation for some reason
2.7557319223985893e-006 // and messes up my results when I use larger input values
2.7557319223985888e-007
2.505210838544172e-008
2.08767569878681e-009
1.6059043836821613e-010
1.1470745597729725e-011
7.6471637318198164e-013
4.7794773323873853e-014
2.8114572543455206e-015
1.5619206968586225e-016
8.2206352466243295e-018
4.1103176233121648e-019
-2.3533342943644858e-019 // Especially this negative number that somehow shows up?
e raised to the 1.00000000000000000000 power is 2.71828182845904550000
The number of terms computed is 21 // I also want to know why it stopped computing at 21 and didn't keep on going until result < 1.0E-20
Press any key to continue . . .
---
Can you tell me why the answer suddenly changed to scientific notation? Or even a negative scientific notation number?