[R] 0.3 is not 0.3, bug in seq() function?
Rubén Roa
rroa at azti.es
Thu Oct 28 12:43:47 CEST 2010
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.
>
More information about the R-help
mailing list