C语言编一个计算e的值的程序

C语言编一个计算e的值的程序,第1张

// double ??

for(i=1.000000n>=EPSILON++i)

    {

        z=z*i // 阶乘

        n=m/z // 求反

        t=t+n // 求和

    }

你是想表达这个意思吧

double s,e,n,z,m,t,i // 1. 不需要全部使用double的

// double是处理小数位数多的时候进行使用的

// double和float都是一个相似值

    t=0.000000

    m=1.000000

    s=1.000000

    z=1.000000

    for(i=1.000000n>=EPSILON++i) // n的初始化呢? n什么时候才能够 >= EPSILON

    // 所以你这里应该使用do while的结构,而不是 for

    {

        z=z*i // 这里的问题,就是我前面标示的,

               //long long会更精确一点,而1.0000001 * 1.0000002的结果会对下面有影响的

        n=m/z // m 始终没有改变过,没有必要使用一个变量:)

               // n = double(1) / z

        t=t+n // t += n 个人习惯

    }

    e=t+1.000000

    printf("e=%.6lf",e)

    return 0

//给你一个高精度的#include <iostream.h>

#include <fstream.h>const int max = 10100

const int m = 5000

int r[max]

int rr[max]void add (int *a, int *b)

{

int tmp = 0, i

for (i=max-1i>=0i--){

tmp += a[i] + b[i]

a[i] = tmp%10

tmp /= 10

}

}int main()

{

ofstream out("a.txt")

int i, tmp, j

for (i=0i<maxi++)

r[i] = 0, rr[i] = 0

rr[0] = 1

r[0] = 1

for (j = 1j <mj++){

tmp = rr[0]

for (i=0i++){

rr[i] = tmp/j

if (i == max - 1)

break

tmp = tmp%j*10 + rr[i+1]

}//for

add (r, rr)

}//for cout<<r[0]<<"."

for (i=1i<max-100i++)

cout<<r[i], out<<r[i]

cout<<endl

out.close()

return 0

}

#include "stdio.h"

void main()

{

int k,j

long m

double e=0

for(k=0k++)

{

// k!

for(j=1,m=1j<=kj++)

{

m*=j

}

e+=1.0/m

if(1.0/m <0.000001)

break

}

printf("e=%lf",e)

printf("\npress any key to exit:\n")

getch()

}

运行结果:

e=2.718282

press any key to exit:


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7717498.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-09
下一篇2023-04-09

发表评论

登录后才能评论

评论列表(0条)

    保存