C++ 用while循环计算数学常数e
我目前正在一本书中做一项任务,要求我使用while循环计算数学常数e。我很容易做到这一点,但是我在计算e^x时遇到困难,而用户输入x和准确度。我用于计算e的代码是:C++ 用while循环计算数学常数e,c++,while-loop,C++,While Loop,我目前正在一本书中做一项任务,要求我使用while循环计算数学常数e。我很容易做到这一点,但是我在计算e^x时遇到困难,而用户输入x和准确度。我用于计算e的代码是: #include <iostream> #include <iomanip> using namespace std; int main() { int degreeOfAccuracy, x = 1; long double e = 1; cout << "Enter degree of ac
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int degreeOfAccuracy, x = 1;
long double e = 1;
cout << "Enter degree of accuracy of mathimatical constant e: ";
cin >> degreeOfAccuracy;
while (x <= degreeOfAccuracy)
{
int conter = x;
int intial = x;
long double number = x;
int counter = 1;
while (conter > 1)
{
number = number*(intial-counter);
counter++;
conter--;
}
e += (1/number);
x++;
}
cout << endl << "The mathematical constantr e is: "
<< setprecision(degreeOfAccuracy) << fixed << e << endl;
system("pause");
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
整数度精度,x=1;
长双e=1;
cout>度精度;
while(x1)
{
编号=编号*(初始计数器);
计数器++;
内容——;
}
e+=(1/个);
x++;
}
cout这条线:
exponent *= exponent;
这是错误的。应该是:
exponent *= temp;
这一行:
exponent *= exponent;
这是错误的。应该是:
exponent *= temp;
它是如何失败的&在哪里失败的?在编译时?运行时?请更具体一些。它没有编译吗?它没有跑吗?输出是否不正确?exp基本上与sin相同,因此如果遇到问题,可以使用。对不起,我将纠正“失败”的原因,它是如何失败的&在哪里失败的?在编译时?运行时?请更具体一些。它没有编译吗?它没有跑吗?输出是否不正确?exp本质上与sin相同,因此如果遇到问题,可以使用。对不起,我将更正“失败”