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
Enrico, The same happens with other numbers/sequences. seq(0.1,0.9,0.1)[7]==0.7 [1] FALSE seq(0.1,1.3,0.1)[12]==1.2 [1] FALSE Rounding seems to fix it, round(seq(0.1,0.5,0.1),1)[3]==0.3 round(seq(0.1,0.9,0.1),1)[7]==0.7 round(seq(0.1,1.3,0.1),1)[12]==1.2 They all return TRUE. ____________________________________________________________________________________ Dr. Rub?n Roa-Ureta AZTI - Tecnalia / Marine Research Unit Txatxarramendi Ugartea z/g 48395 Sukarrieta (Bizkaia) SPAIN> -----Mensaje original----- > De: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] En nombre de Enrico R. Crema > Enviado el: jueves, 28 de octubre de 2010 12:24 > Para: r-help at r-project.org > Asunto: [R] 0.3 is not 0.3, bug in seq() function? > > 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 > > ______________________________________________ > 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. >
-----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-----
>>> "Enrico R. Crema" <enryu_crema at yahoo.it> 28/10/2010 12:08:55 >>> >Well...thanks to everybody for suggestions and reading...I guess Ihave to study more!> ... >> On 28/10/10 12:23, Enrico R. Crema wrote: >> .... >> I can't provide you a link for more details,The canonical link is R FAQ 7.31 in the 'Frequently Asked Questions' issued as part of the R HTML Help system. Steve E ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}