为什么小数字的答案是错误的?与MPIR

时间:2013-10-29 23:21:41

标签: c++ c rounding rounding-error

我正在使用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;

这是p [i]和probLessThanR的一些值的输出:

    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的所有答案都应该是正面的。我更喜欢积极而不准确而不是消极(但准确性非常重要)。

任何想法?

编辑:将输出添加为文本和a的值。顺便说一句,a长的原因很长(它可以有更大的价值)。

1 个答案:

答案 0 :(得分:0)

不,那是对的。

非常感谢屏幕截图 。比单纯的剪裁和艺术更具艺术性。糊。毕竟谁在乎可用性?

如果(1-p[I])^a0.99999999999783207310676356492969

probLessThanR2.1679268932410045e-12

然后probLessThanR0.0000000000021679268932410045

所以使用小学附加:

(1-p[I])^a + probLessThanR是,

      0.99999999999783207310676356492969 
    + 0.0000000000021679268932410045` 
    ~ 0.9999999999999999999999045684...
Carry                         1    1
    = 1.0000000000000000000000045694...

所以1-(1-p[I])^a - probLessThanR0.0000000000000000000000045694...。这就是你拥有的。