问题描述
解决方案
改用double
调整计算顺序,不要先乘再除,尽量避免乘除法的精度损失
解决方案二:
#include <iostream>
using namespace std;
double f1(int n)
{
double r = 1;
for (int i = 2; i <= n; i++)
r *= (double)i;
return r;
}
double f2(double x, int n)
{
double r = 1;
for (int i = 1; i <= n; i++)
r *= x;
return r;
}
double f3(int n, double x)
{
double u = 0.5;
for (int i = 1; i < n; i++)
{
u *= (0.5 - i);
}
return u * f2(x, n) / f1(n);
}
int main()
{
double ps = 1;
double x = 0.21;
int n = 1;
while (1)
{
double s = ps + f3(n, x);
if (abs(s - ps) < 0.000001) break;
ps = s;
}
cout << ps << endl;
}
解决方案三:
你没有贴你的代码出来,怎么让人分析你的问题呢?
时间: 2024-10-29 10:33:58