它应该评估 e^pi - pi。
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
long double Pie();
long double Factorial(double n);
long double E();
int main()
{
long double answer = pow(E(),Pie()) - Pie();
cout << setprecision(20);
cout << answer;
return 0;
}
long double Pie()
{
long double a = 1;
long double b = (1 / sqrtl(2));
long double t = (1.0 / 4.0);
long double p = 1;
long double aPlaceholder;
for (int i = 1; i < 5; i++)
{
aPlaceholder = a;
a = (a + b) / 2;
b = sqrtl(aPlaceholder * b);
t = t - p * (aPlaceholder - a) * (aPlaceholder - a);
p = 2 * p;
}
long double nicePie;
nicePie = (a + b) * (a + b) / (4 * t);
return nicePie;
}
long double E()
{
long double e = 0;
for(double i = 0; i < 20; i++)
e += 1.0 / Factorial(i);
return e;
}
long double Factorial(double n)
{
if(n == 0)
return 1;
int i = n - 1;
while (i > 0)
{
n *= i;
i--;
}
return n;
}
场景是我想评估 e,将其提升到 pi 的幂,然后从结果中减去 pi,然后将答案打印到屏幕上。该场景的另一个方面是这是一个基本的 C++ 程序。