使用 while 循环计算数学常数 e

发布于 2024-11-18 08:16:20 字数 1933 浏览 1 评论 0原文

我目前正在做一本书中的一项任务,要求我使用 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 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;
}

但是,当我尝试 e^x 时,以下代码返回了一个完全错误的值:

#include <iostream>
#include <iomanip>
using namespace std; 

int main()
{
int degreeOfAccuracy, x = 1, exponent;
long double e = 1;
cout << "Enter degree of accuracy of mathimatical constant e: ";
cin >> degreeOfAccuracy;
cout << "Enter the number of which you wish to raise to e: ";
cin >> exponent;
int temp = exponent;
while (x <= degreeOfAccuracy)
{
      exponent = temp;
      int conter = x;
      int intial = x;
      long double number = x;
      int counter = 1;
      while (conter > 1)
      {
             number = number*(intial-counter);
             counter++;
             conter--;
      }
      int counterr = 1;
      while (counterr < x)
      {
            exponent *= exponent;
            counterr++;
      }
      e += (exponent/number);
      x++;
}
cout << endl << "The mathematical constantr e is: " << setprecision(degreeOfAccuracy) << fixed << e << endl;
system("pause");
return 0;
}

有什么想法计算出了问题吗?

I am currently doing a task in a book which asks me to calculate the mathematical constant e using the while loop. I managed that fairly easily, however I am having troubles calculating e^x, whereas the user inputs x and the degree of accuracy. The code I used for computing e is:

#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;
}

However, when I tried e^x the following code returned a completely wrong value:

#include <iostream>
#include <iomanip>
using namespace std; 

int main()
{
int degreeOfAccuracy, x = 1, exponent;
long double e = 1;
cout << "Enter degree of accuracy of mathimatical constant e: ";
cin >> degreeOfAccuracy;
cout << "Enter the number of which you wish to raise to e: ";
cin >> exponent;
int temp = exponent;
while (x <= degreeOfAccuracy)
{
      exponent = temp;
      int conter = x;
      int intial = x;
      long double number = x;
      int counter = 1;
      while (conter > 1)
      {
             number = number*(intial-counter);
             counter++;
             conter--;
      }
      int counterr = 1;
      while (counterr < x)
      {
            exponent *= exponent;
            counterr++;
      }
      e += (exponent/number);
      x++;
}
cout << endl << "The mathematical constantr e is: " << setprecision(degreeOfAccuracy) << fixed << e << endl;
system("pause");
return 0;
}

Any ideas where the calculations went wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-11-25 08:16:20

这行:

exponent *= exponent;

是错误的。应该是:

exponent *= temp;

This line:

exponent *= exponent;

is wrong. It should be:

exponent *= temp;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文