0

I'm using MPIR to deal with extremely small numbers. Somehow the answers I'm getting are wrong, and I have no idea why (something with rounding I guess..). How does rounding work in MPIR, and is it the reason for me getting these wrong answers?

Here is the code (the relevant part):

    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;

And here is the output for some values of p[i] and 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

All the answers of 1-(1-p[i])^a-probLessThanR should be positive. I prefer positive and less accurate than negative (but accuracy is really important).

Any Ideas?

Edit: added the output as a text and the value of a. BTW, a is long long for a reason (it can have bigger values).

4

1 回答 1

0

No, that's correct.

Thanks so much for the screenshot. Much more artistic than a mere cut & paste. Who cares about usability after all?

If (1-p[I])^a is 0.99999999999783207310676356492969

And probLessThanR is 2.1679268932410045e-12

Then probLessThanR is 0.0000000000021679268932410045

So using elementary school addition with carry:

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

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

So 1-(1-p[I])^a - probLessThanR is 0.0000000000000000000000045694.... Which is what you have.

于 2013-10-29T23:56:49.040 回答