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

前端 未结 2 763
鱼传尺愫
鱼传尺愫 2021-01-12 23:34

I have the problem that a sum of variables representing probabilities in the [0,1] interval sum to 0 but should be >0. The problem

2条回答
  •  有刺的猬
    2021-01-13 00:03

    R itself can only deal with 64 bit floating point numbers, the Rmpfr package can deal with arbitrary precision floating point numbers:

    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
    

    Edit: Use stings to define mpfr

提交回复
热议问题