[R] 0.3 is not 0.3, bug in seq() function?

Enrico R. Crema enryu_crema at yahoo.it
Thu Oct 28 12:23:32 CEST 2010


Dear List,

I've been running a numerical simulation and I found this odd error in my code where the which command could not identify which rows of a column of data.frame were corresponding to the value 0.3. There are 7 unique values in this column (0.01,0.05,0.1,0.2,0.3,0.4,0.5), and this does not work only for 0.3. So I looked at the column and manually tried to use the which() command, and the results were all FALSE despite I could see those number. So I recreated my sequence of number and tested:

seq(0.1,0.5,0.1)[3]==0.3

which gave me FALSE!!! All the other numbers (0.1,0.2,0.4,0.5) give me TRUE, but 0.3 was not working. So I did:

seq(0.1,0.5,0.1)[3]-0.3

which gave me 5.551115e-17. If you run a similar sequence like:

seq(0.2,0.6,0.1)[2]==0.3

this will still give me FALSE. No, for my own purpose, I fixed the problem in this way:

zerothree=seq(0.1,0.5,0.1)[3]
which(data[,1]==zerothree)

but I guess this bug is a bit of problem...Apologies if it is the wrong place to post this bug, and apologies also if this was a known issue. My version of R is :

platform       x86_64-pc-linux-gnu          
arch           x86_64                       
os             linux-gnu                    
system         x86_64, linux-gnu            
status                                      
major          2                            
minor          10.1                         
year           2009                         
month          12                           
day            14                           
svn rev        50720                        
language       R                            
version.string R version 2.10.1 (2009-12-14)


Many Thanks,

Enrico



More information about the R-help mailing list