bracketslash
Registered User
- Joined
- Feb 6, 2013
- Messages
- 224
Here's a little C++ example I threw together to use a Maclaurin series to estimate e^x.
And here's the pastebin for formatting: [C++] int tayExp() { int choice, maxLoops=10; double number = 1; int it - Pastebin.com
int tayExp() {
int choice, maxLoops=10;
double number = 1;
int iter, i;
float result=1.0f;
double fact=1.0, pow=1.0;
cout << "To what degree would you like to estimate e?: ";
cin >> number;
cout << endl << "Doing some math..." << endl;
result = 0.000000000000f;
iter = 0;
while ( iter < maxLoops ) {
fact = 1;
pow = 1;
for ( i = 0; i < iter; i++ ) {
pow *= number;
}
for ( i = iter; i > 1; i-- ) {
fact *= i;
}
result += pow/fact;
iter++;
cout << "*" << iter << " * " << result << "*" << endl;
if ( (iter % 9) == 0 ) {
cout << "Would you like to see ten more? (1 for yes, 0 for no): ";
cin >> choice;
switch ( choice ) {
case 0:
return 0;
break;
case 1:
maxLoops += 10;
continue;
break;
}
}
}
}
And here's the pastebin for formatting: [C++] int tayExp() { int choice, maxLoops=10; double number = 1; int it - Pastebin.com