---
# System prepended metadata

title: C習題3-46-c
tags: [C習題]

---

# C習題3-46-c
###### tags: `C習題`
## 題目:
> 寫一個程式計算e^x值
## code:
```c=
#include <stdio.h>

int main()
{
    int n = 1, k = 1; /*n to store n! value, e to store exp value, k as counter*/
    double e =1,x;
    float xm;
    
    printf("Input x value to calc e^x :\n");
    scanf("%lf",&x);
    xm = x ;
    
    while( k <= 12 ) {
        n = n *  k ;
        
        printf("n = %d\n",n);
        /*printf("1/n = %f\n", (float) 1/n);*/
        printf("x before = %lf\n",x);
        e = e + ((float) x / n ) ; /*int to float transfer, otherwise below decible will not add in*/
        printf("e = %.6lf\n",e);
        x = xm * x ;
        printf("x after = %lf\n",x);
        k = k + 1 ;
        printf("k = %d\n", k);
    
    }
    printf("e^x value = %.6lf\n",e);
    
    
    return 0;
}
```
## 心得
> 重複結構設定只跑12次, 結果如下
> x = 1 value = 2.718282 ; 計算機按出:2.718281
> x = 2 value = 7.389055 ; 計算機按出:7.389056
> x = 3 value = 20.085213; 計算機按出:20.085536
> x = 4 value = 54.583207; 計算機按出:54.598150
> x = 5 value = 148.113534; 計算機按出:148.413159
1. 寫出程式與計算機結果在指數值越大則差越多, 
2. 當x 值越大或重複結構次數越多, 會遇到值溢位問題...
3. 宣告的種類沒有特別想, 只想著把足夠記憶體開足
4. 這題拿e的來改, 只加入x的敘述而已; xm用來記錄剛輸入的指數初始值