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

Rainer M Krug r.m.krug at gmail.com
Thu Oct 28 12:48:01 CEST 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 28/10/10 12:23, Enrico R. Crema wrote:
> 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)

This is not a bug, but based on the internal representation of numbers
in the binary format. Numbers like 0.3 can not be represented as exactly
0.3, wherefore errors accumulate in a sequence.

I can't provide you a link for more details, but the topic comes up
every now and then in this mailing list.

Cheers,

Rainer

> 
> 
> Many Thanks,
> 
> Enrico
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Natural Sciences Building
Office Suite 2039
Stellenbosch University
Main Campus, Merriman Avenue
Stellenbosch
South Africa

Tel:        +33 - (0)9 53 10 27 44
Cell:       +27 - (0)8 39 47 90 42
Fax (SA):   +27 - (0)8 65 16 27 82
Fax (D) :   +49 - (0)3 21 21 25 22 44
Fax (FR):   +33 - (0)9 58 10 27 44
email:      Rainer at krugs.de

Skype:      RMkrug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkzJVOEACgkQoYgNqgF2egpVXACeNO9GyRh9XKLg1g4EhcFs4Qa5
WXgAmgOdNB/xgmRS4UsMaEtunxS6NB/u
=azme
-----END PGP SIGNATURE-----



More information about the R-help mailing list