numeric comparison error

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

numeric comparison error

Brian Smith
Hi,

I am a little bit perplexed at why I am getting some values as FALSE:

> cpgbins <- seq(0,1,0.05)

> cpgbins
 [1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65
0.70 0.75 0.80 0.85 0.90 0.95 1.00

> cpgbins[1] == 0.00
[1] TRUE
> cpgbins[2] == 0.05
[1] TRUE
> cpgbins[3] == 0.10
[1] TRUE
> cpgbins[4] == 0.15
[1] FALSE
> cpgbins[5] == 0.20
[1] TRUE
> cpgbins[6] == 0.25
[1] TRUE
> cpgbins[7] == 0.30
[1] FALSE

> class(cpgbins)
[1] "numeric"

> class(cpgbins[7])
[1] "numeric"

What is the cause for this?

thanks!!

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: numeric comparison error

Jeff Newmiller
FAQ 7.31, or take a university course in numerical analysis.

https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

On June 18, 2018 2:45:29 PM PDT, Brian Smith <[hidden email]> wrote:

>Hi,
>
>I am a little bit perplexed at why I am getting some values as FALSE:
>
>> cpgbins <- seq(0,1,0.05)
>
>> cpgbins
>[1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60
>0.65
>0.70 0.75 0.80 0.85 0.90 0.95 1.00
>
>> cpgbins[1] == 0.00
>[1] TRUE
>> cpgbins[2] == 0.05
>[1] TRUE
>> cpgbins[3] == 0.10
>[1] TRUE
>> cpgbins[4] == 0.15
>[1] FALSE
>> cpgbins[5] == 0.20
>[1] TRUE
>> cpgbins[6] == 0.25
>[1] TRUE
>> cpgbins[7] == 0.30
>[1] FALSE
>
>> class(cpgbins)
>[1] "numeric"
>
>> class(cpgbins[7])
>[1] "numeric"
>
>What is the cause for this?
>
>thanks!!
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: numeric comparison error

Bert Gunter-2
In reply to this post by Brian Smith
FAQ 7.31.

Binary arithmetic.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Mon, Jun 18, 2018 at 2:45 PM, Brian Smith <[hidden email]> wrote:

> Hi,
>
> I am a little bit perplexed at why I am getting some values as FALSE:
>
> > cpgbins <- seq(0,1,0.05)
>
> > cpgbins
>  [1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65
> 0.70 0.75 0.80 0.85 0.90 0.95 1.00
>
> > cpgbins[1] == 0.00
> [1] TRUE
> > cpgbins[2] == 0.05
> [1] TRUE
> > cpgbins[3] == 0.10
> [1] TRUE
> > cpgbins[4] == 0.15
> [1] FALSE
> > cpgbins[5] == 0.20
> [1] TRUE
> > cpgbins[6] == 0.25
> [1] TRUE
> > cpgbins[7] == 0.30
> [1] FALSE
>
> > class(cpgbins)
> [1] "numeric"
>
> > class(cpgbins[7])
> [1] "numeric"
>
> What is the cause for this?
>
> thanks!!
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: numeric comparison error

R help mailing list-2
In reply to this post by Jeff Newmiller
What Jeff, said, plus to see it explicitly:

> print(cpgbins[5:7], digits=18)
[1] 0.200000000000000011 0.250000000000000000 0.300000000000000044

> print(c(0.2, 0.25, 0.3), digits=18)
[1] 0.200000000000000011 0.250000000000000000 0.299999999999999989

-Don

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

´╗┐On 6/18/18, 2:53 PM, "R-help on behalf of Jeff Newmiller" <[hidden email] on behalf of [hidden email]> wrote:

    FAQ 7.31, or take a university course in numerical analysis.
   
    https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
   
    On June 18, 2018 2:45:29 PM PDT, Brian Smith <[hidden email]> wrote:
    >Hi,
    >
    >I am a little bit perplexed at why I am getting some values as FALSE:
    >
    >> cpgbins <- seq(0,1,0.05)
    >
    >> cpgbins
    >[1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60
    >0.65
    >0.70 0.75 0.80 0.85 0.90 0.95 1.00
    >
    >> cpgbins[1] == 0.00
    >[1] TRUE
    >> cpgbins[2] == 0.05
    >[1] TRUE
    >> cpgbins[3] == 0.10
    >[1] TRUE
    >> cpgbins[4] == 0.15
    >[1] FALSE
    >> cpgbins[5] == 0.20
    >[1] TRUE
    >> cpgbins[6] == 0.25
    >[1] TRUE
    >> cpgbins[7] == 0.30
    >[1] FALSE
    >
    >> class(cpgbins)
    >[1] "numeric"
    >
    >> class(cpgbins[7])
    >[1] "numeric"
    >
    >What is the cause for this?
    >
    >thanks!!
    >
    > [[alternative HTML version deleted]]
    >
    >______________________________________________
    >[hidden email] mailing list -- To UNSUBSCRIBE and more, see
    >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.
   
    --
    Sent from my phone. Please excuse my brevity.
   
    ______________________________________________
    [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    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.
   

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.