How can I increase accuracy in R when calculating with probabilities close to 0 and 1?

I have a problem that the sum of the variables representing the probabilities in the interval [0,1]is cumulative with 0, but should be >0. The problem is undoubtedly related to the floating point representation and precision in R, but I cannot determine where this is happening.

options(digits = 22)
p1 = 0.8
p2 = 0.9999999999999998

p11 = p1 * p2
p10 = p1 - p11
p01 = p2 - p11
p00 = 1 - p11 - p10 - p01

p11, p10, p01that's all numeric. p00also numericbut

> p00
[1] 0

and p00 == 0in my car TRUE. However, it should not be zero, since it can be shown p00mathematically >0.

, -, , p01 . p01>0 TRUE . p00?

, .. p00? p - [0,1]. log exp, .

+4
2

R 64- , Rmpfr :

library(Rmpfr)

> p1 = mpfr("0.8", 128)
> p2 = mpfr("0.9999999999999998", 128)

> p11 = p1 * p2
> p10 = p1 - p11
> p01 = p2 - p11
> p00 = 1 - p11 - p10 - p01

> p00
1 'mpfr' number of precision  128   bits 
[1] 4.00000000000000000000000461461738779728e-17

: stings mpfr

+6

, : .

- , ..

> p1 = 0.8
> p2 = 0.9999999999999998
> (1-p1)*(1-p2)
[1] 4.440892e-17

, 0.9999999999999998 0.9999999999999998, 0.9999999999999997779553950749686919152736663818359375 ( )

: , .

, : . , ( , ).

, 2 :

  • .

: , 1e-16 ( . , ( 2 , 2x , 2).

, , : , : , . , .

, . , ( p2 , "0.9999999999999998" ).

+2

Source: https://habr.com/ru/post/1694825/


All Articles