[R] odd behavior of seq()

Peter Langfelder peter.langfelder at gmail.com
Thu Jul 3 20:38:42 CEST 2014


Precision, precision, precision...

> z[2]-0.15
[1] 2.775558e-17

My solution:

> z <- signif(seq(.05,.85,by=.1), 5)
> z[2] - 0.15
[1] 0
> z[2]==0.15
[1] TRUE

Peter

On Thu, Jul 3, 2014 at 11:28 AM, Matthew Keller <mckellercran at gmail.com> wrote:
> Hi all,
>
> A bit stumped here.
>
> z <- seq(.05,.85,by=.1)
> z==.05     #good
> [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>
> z==.15  #huh????
> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>
> More generally:
>> sum(z==.25)
> [1] 1
>> sum(z==.35)
> [1] 0
>> sum(z==.45)
> [1] 1
>> sum(z==.55)
> [1] 1
>> sum(z==.65)
> [1] 0
>> sum(z==.75)
> [1] 0
>> sum(z==.85)
> [1] 1
>
> Does anyone have any ideas what is going on here?
>
>> R.Version()
> $platform
> [1] "x86_64-apple-darwin9.8.0"
>
> $arch
> [1] "x86_64"
>
> $os
> [1] "darwin9.8.0"
>
> $system
> [1] "x86_64, darwin9.8.0"
>
> $status
> [1] ""
>
> $major
> [1] "2"
>
> $minor
> [1] "13.1"
>
> $year
> [1] "2011"
>
> $month
> [1] "07"
>
> $day
> [1] "08"
>
> $`svn rev`
> [1] "56322"
>
> $language
> [1] "R"
>
> $version.string
> [1] "R version 2.13.1 (2011-07-08)"
>
> --
> Matthew C Keller
> Asst. Professor of Psychology
> University of Colorado at Boulder
> www.matthewckeller.com
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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