我在做一個計算彩票概率的程序。 規格是選擇5個號碼進行的47進1出的27long double vs long int
所以我做了以下內容:
#include <iostream>
long int choose(unsigned n, unsigned k);
long int factorial(unsigned n);
int main(){
using namespace std;
long int regularProb, megaProb;
regularProb = choose(47, 5);
megaProb = choose(27, 1);
cout << "The probability of the correct number is 1 out of " << (regularProb * megaProb) << endl;
return 0;
}
long int choose(unsigned n, unsigned k){
return factorial(n)/(factorial(k) * factorial(n-k));
}
long int factorial(unsigned n){
long int result = 1;
for (int i=2;i<=n;i++) result *= i;
return result;
}
然而,程序不能正常工作。該程序計算30秒,然後給我Process 4 exited with code -1,073,741,676
我必須將所有long int更改爲long double,但是失去精度。是否因爲long int對於大值太短?儘管現在我認爲long int是64bit?我的編譯器是g ++ win32(64位主機)。
原因是:數據overflowwww ... – Nawaz 2011-05-10 15:55:43