title: How can I increase precision in R when calculating with probabilities close to 0 and 1?
body: <p>I have the problem that a sum of variables representing probabilities in the <code>[0,1]</code> interval sum to <code>0</code> but should be <code>&gt;0</code>. The problem is surely related to <a href="https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate">floating point representation</a> and precision in <code>R</code>, but I cannot pin down where it goes wrong.</p>
  
  <pre><code>options(digits = 22)
  p1 = 0.8
  p2 = 0.9999999999999998
  
  p11 = p1 * p2
  p10 = p1 - p11
  p01 = p2 - p11
  p00 = 1 - p11 - p10 - p01
  </code></pre>
  
  <p><code>p11, p10, p01</code> all are <code>numeric</code>. <code>p00</code> is also <code>numeric</code> but </p>
  
  <pre><code>&gt; p00
  [1] 0
  </code></pre>
  
  <p>and <code>p00 == 0</code> is <code>TRUE</code> on my machine. However it should not be zero as it can be shown that <code>p00</code> is <code>&gt;0</code> mathematically. </p>
  
  <p>This problem seems to be related to the fact that <code>p01</code> is small. However <code>p01&gt;0</code> is <code>TRUE</code> still applies on my machine. Why does it go wrong when taking the final sum in <code>p00</code>? </p>
  
  <p>Is there a numerical trick to solve this problem, i.e. get an exact representation of <code>p00</code>? Note again <code>p</code> are probabilities in <code>[0,1]</code>. I considered using <code>log</code> and <code>exp</code> transformations but without consistent success. </p>