[問題] 一個小測試
請問一下
int 所設的數是不是太大的話,程式會爆掉
我是c++菜鳥。
以下是我寫的一個測試pi的c++程式
上面include的東西我就不打了比較特別的應該只有<cmath>
int main() {
long double temp;
int n;
for (;;){
temp =0;
cout<<"enter a number : "<<endl;
cin>>n;
for (int k=0;k<=n;k++){
temp = temp + pow(-1.0,k)/long double(2*k+1);
}
temp = 4*temp;
cout<<temp<<endl;
}
return 0;
}
目前這個遇到的問題是,當int n 很大的時候,例如1000000000
它就會溢出。
另一個問題是,我希望它可以顯示出小數點後很多位數。
但是,它只跑出3.14159頂多到這裡就沒了。
請問有其它方法可以解嗎?
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 109.246.227.44
推
12/04 09:03, , 1F
12/04 09:03, 1F
→
12/04 09:03, , 2F
12/04 09:03, 2F
推
12/04 09:05, , 3F
12/04 09:05, 3F
→
12/04 09:06, , 4F
12/04 09:06, 4F
→
12/04 09:07, , 5F
12/04 09:07, 5F