你好同学,代码供参考
s=1; ds = 1; n=1; f=1; while(ds>1e-5) s = s+ds; n=n+1; f=f*n; ds = 1/f; end fprintf('s=%.5f, error=%.5f\n', s, (s-exp(1))/exp(1))
结果:
s=2.71828, error=-0.00000