我使用MPIR來處理極小的數字。不知何故,我得到的答案是錯誤的,我不知道爲什麼(我猜想四捨五入的東西..)。 MPIR中的舍入是如何工作的,這是我得到這些錯誤答案的原因嗎?爲什麼小數字的答案是錯誤的?與MPIR
這裏是代碼(相關部分):
long long a = 100000;
mpf_class calc(p[i],500);
cout << "p[i] = " << setprecision(32) << calc << endl;
calc = 1-calc;
cout << "1-p[i] = " << setprecision(32) << calc << endl;
mpf_pow_ui(calc.get_mpf_t(), calc.get_mpf_t(), a);
cout << "(1-p[i])^a = " << setprecision(32) << calc << endl;
cout << "probLessThanR = " << setprecision(32) << probLessThanR << endl;
calc = 1-calc-probLessThanR;
cout << "1-(1-p[i])^a-probLessThanR = " << setprecision(32) << calc << endl;
if (calc>0)
cout << "calc>0 = " << 1 << endl;
這裏是[i]和probLessThanR P的一些值輸出:
p[i] = 2.0432284241450287639483056612667e-17
1-p[i] = 0.99999999999999997956771575854971
(1-p[i])^a = 0.99999999999795677157585705860637
probLessThanR = 2.0432284241428158e-012
1-(1-p[i])^a-probLessThanR = 1.2561170838194078535224341399684e-25
calc>0 = 1
p[i] = 2.1679268932387850003127872242701e-17
1-p[i] = 0.99999999999999997832073106761215
(1-p[i])^a = 0.99999999999783207310676356492969
probLessThanR = 2.1679268932410045e-012
1-(1-p[i])^a-probLessThanR = -4.5694136331284619232701251208227e-24
p[i] = 2.2996656655640389938724454087815e-17
1-p[i] = 0.99999999999999997700334334435961
(1-p[i])^a = 0.99999999999770033433443860521077
probLessThanR = 2.2996656655715272e-012
1-(1-p[i])^a-probLessThanR = -1.0132363051975571461595673730287e-23
p[i] = 2.4388090428503683876184122197242e-17
1-p[i] = 0.99999999999999997561190957149632
(1-p[i])^a = 0.99999999999756119095715260547742
probLessThanR = 2.4388090428370166e-012
1-(1-p[i])^a-probLessThanR = 1.0377918963850787511442329601381e-23
calc>0 = 1
所有1-(1-p[i])^a-probLessThanR
的答案應該要樂觀。我比較喜歡積極和不太準確(但準確性非常重要)。
任何想法?
編輯:添加輸出爲文本和值的一個。順便說一句,一個很長的原因(它可以有更大的價值)。
複製粘貼您的輸出,而不是對其進行屏幕分析。這樣看起來很煩人,有些人可能沒有啓用圖像,也不能用於引用可能答案中的輸出。 – Kninnug
什麼是var a? – Plasmarob
編輯並添加了您要求的內容,謝謝。長長的a = 100000; –