[R] (1 - 0.7) == 0.3

arun smartpink111 at yahoo.com
Tue Jul 16 20:10:10 CEST 2013


HI,
2-0.7==0.3
#[1] FALSE
##May be u meant
 2-0.7==1.3
#[1] TRUE


Possibly R FAQ 7.31
Also, check
http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy

all.equal(2-0.7,1.3)
#[1] TRUE
 all.equal(1-0.7,0.3)
#[1] TRUE
(1-0.7)<(0.3+.Machine$double.eps^0.5)
#[1] TRUE



 p <- c(0.2, 0.4, 0.6, 0.8, 1) 
round((1-p)*5,1)+1
#[1] 5 4 3 2 1


In your second example,
 p <- c(0.8, 0.6, 0.4, 0.2, 0)
floor((1 - p) * 5) + 1 
#[1] 1 3 4 5 6  
((1-0.8)*5) +1
#[1] 2


 round((1-p)*5,1)+1
#[1] 2 3 4 5 6

A.K.

...is false :( However (2 - 0.7) == 0.3 is true. 

Is there any way to get around this?   

The end goal is for this to work: 

p <- c(0.2, 0.4, 0.6, 0.8, 1) 
floor((1 - p) * 5) + 1 

>  5 4 3 1 1 

whereas the correct result would have been 5 4 3 2 1. If I set p <- c(0.8, 0.6, 0.4, 0.2, 0) then it works as expected.



More information about the R-help mailing list