[Rd] Accuracy (PR#13867)

Petr Savicky savicky at cs.cas.cz
Tue Aug 4 20:27:44 CEST 2009


On Tue, Aug 04, 2009 at 04:25:09PM +0200, luethman at student.ethz.ch wrote:
> Hi
> 
> I created the following vectors:
> 
> p_1=c(0.2,0.2,0.2,0.2,0.1,0.25,0.4,0.1,0.25,0.4,0.1,0.25,0.4,0.1,0.25,0.4,0.2,0.5,0.8,0.2,0.5,0.8,0.2,0.5,0.8,0.2,0.5,0.8)
> p_2=c(0,0,0,0,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.4,0.25,0.1,0.4,0.25,0.1,0.4,0.25,0.1,0.4,0.25,0.1)
> 
> As these are probabilities, I calculated the remainder as
> 
> p_3=1-p_1-p_2
> 
> There the values which ought to be 0.1 were lateron not recognised by p_3==0.1,
> but only if I used p_3 <= 0.1.
> 
> The full calculation is actually bigger but the core issue remains: I used
> values input by hand, built a difference and it was wrong.
> 
> I know that exactly the value 0.1 is one that can not be represented using
> binary rep. Maybe that's it, maybe not.

Yes, this is the problem. In this case, one can obtain a correct
result using round()

  p1 <- c(0.2,0.2,0.2,0.2,0.1,0.25,0.4,0.1,0.25,0.4,0.1,0.25,0.4,0.1,
          0.25,0.4,0.2,0.5,0.8,0.2,0.5,0.8,0.2,0.5,0.8,0.2,0.5,0.8)
  p2 <- c(0,0,0,0,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.4,
          0.25,0.1,0.4,0.25,0.1,0.4,0.25,0.1,0.4,0.25,0.1)
  p3 <- 1 - p1 - p2
  round(p3, 2) == 0.1
  #  [1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
  # [13]  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
  # [25]  TRUE FALSE FALSE  TRUE

Petr.



More information about the R-devel mailing list