對於尤拉數,

我們可以使用 Power series 對原點展開,將會變成..

e = 1 + 1/1! + 1/2! + 1/3! + 1/4!,

同樣的,如果使用 C 語言來實做時會遇到一個問題...50! 表示不出來..

這些屬於大數的問題,日後有機會再為各位解釋

其實如果只展開前十項的話,也真的夠了,準確度到小數後第7位。

原始碼如下所示

// ====================================
// FileName: e.cpp
// Author  : Edison.Shih.
// Complier: VC 2008

#include <stdio.h>
#include <stdlib.h>

// ====================================
// e = 2.71828 18284 59045 23536 02874

// ====================================
// Fact(n) = n!
unsigned long Fact(unsigned long n){
        unsigned long i=1;
        unsigned long result = 1;
        for(i=1; i<=n; i++) result = result*i;
        return result;
}

// ====================================
// e = 1 + 1/1! + 1/2! + 1/3! +....
double ConstExp(unsigned long n){
        double ret = 1.0;
        unsigned long i=0;
        for(i=1; i<=n; i++) ret = ret + (1.0/Fact(i)) ;
        return ret;
}

int main(int argc, char **argv)
{
        printf("ConstExp(10) = %lf\n", ConstExp(10)); // 展開前十項
        return 0;
}
// ====================================

如果只單純的看六位小數應該是看不出哪裡有誤差,

倒是可以把 printf("ConstExp(10) = %lf\n", ConstExp(10)); 這行改為

printf("ConstExp(10) = %.15lf\n", ConstExp(10));

那麼就會感覺到誤差在哪了...

arrow
arrow
    全站熱搜

    Edison 發表在 痞客邦 留言(1) 人氣()