用C计算e^1

用C计算e^1,c,series,C,Series,我试图使用C编程(输入公差级别)计算e^1,这是可行的,但由于某些原因,我得到了如下错误输出: exp到0.000100的误差容限:2.000000 exp到0.000000的误差容限:2.000000 exp到0.000000的误差容限:2.000000 exp到0.000000的误差容限:2.000000 有什么想法吗 #include <stdio.h> #include <stdlib.h> //this function creates factorial of

我试图使用C编程(输入公差级别)计算e^1,这是可行的,但由于某些原因,我得到了如下错误输出:

exp到0.000100的误差容限:2.000000

exp到0.000000的误差容限:2.000000

exp到0.000000的误差容限:2.000000

exp到0.000000的误差容限:2.000000

有什么想法吗

#include <stdio.h>
#include <stdlib.h>
//this function creates factorial of n 
int myfac(n)
{
if (n <= 1) return 1;
return n*myfac(n - 1);
}
void myexp(float error)
{
int n = 0;
float value = 0;
//n, n being used for recursion
//error, user input error for tolerance
//value, exp being returned as an answer
//nextvalue, value at n+1
while(1/myfac(n) >= error)
{
    value = value + 1 / myfac(n);
    n = n + 1;
}
printf("The exp to %f error tolerance: %f \n", error, value);
}
void main()
{
    myexp(0.0001);
    myexp(0.0000001);
    myexp(0.0000000001);
    myexp(0.0000000000001);


}
#包括
#包括
//此函数创建n的阶乘
int myfac(北)
{
如果(n=错误)
{
值=值+1/myfac(n);
n=n+1;
}
printf(“扩展到%f的错误容差:%f\n”,错误,值);
}
void main()
{
myexp(0.0001);
myexp(0.0000001);
myexp(0.0000000001);
myexp(0.0000000000001);
}
1/myfac(n)
中,分数的两边都是整数。如果
n
大于1,结果将四舍五入为零


试试
1./myfac(n)
,用小数点表示
double
数字类型。

您计算阶乘的方法非常低效,因为您对每个
myfac(j)
使用
j>=i