Hi all, Is there any problem of precision when using seq?. For example: x<- seq(0,4,0.1) x[4]=0.3 BUT: x[4]-0.3=5.551115e-17 It means when I use this condition within an if clause, it does not find values with 0.3 for x[4] as it is not precisely 0.3. Is there any bug in seq() ? -- View this message in context: http://r.789695.n4.nabble.com/Inconsistency-using-seq-tp4633739.html Sent from the R help mailing list archive at Nabble.com.
Le lundi 18 juin 2012 ? 12:58 -0700, hamoreno a ?crit :> Hi all, > > Is there any problem of precision when using seq?. For example: > > x<- seq(0,4,0.1) > x[4]=0.3 > > BUT: > > x[4]-0.3=5.551115e-17 > > It means when I use this condition within an if clause, it does not find > values with 0.3 for x[4] as it is not precisely 0.3. > Is there any bug in seq() ?No. Have a look at the FAQ: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f Cheers
Please read R FAQ 7.31. The problem is with your computer (and everyone else's), and has nothing to do with seq(). On Mon, Jun 18, 2012 at 3:58 PM, hamoreno <hamoreno at asu.edu> wrote:> Hi all, > > Is there any problem of precision when using seq?. For example: > > x<- seq(0,4,0.1) > x[4]=0.3 > > BUT: > > x[4]-0.3=5.551115e-17 > > It means when I use this condition within an if clause, it does not find > values with 0.3 for x[4] as it is not precisely 0.3. > Is there any bug in seq() ? >-- Sarah Goslee http://www.functionaldiversity.org
R. Michael Weylandt <michael.weylandt@gmail.com>
2012-Jun-18 20:54 UTC
[R] Inconsistency using seq
No, this is rather the nature of floating point calculations. You may perhaps be looking for ?all.equal or R FAQ 7.31 (I think that's the one) which is google-able. It's a complicated subject, but those should get you started. Best, Michael On Jun 18, 2012, at 2:58 PM, hamoreno <hamoreno at asu.edu> wrote:> Hi all, > > Is there any problem of precision when using seq?. For example: > > x<- seq(0,4,0.1) > x[4]=0.3 > > BUT: > > x[4]-0.3=5.551115e-17 > > It means when I use this condition within an if clause, it does not find > values with 0.3 for x[4] as it is not precisely 0.3. > Is there any bug in seq() ? > > > -- > View this message in context: http://r.789695.n4.nabble.com/Inconsistency-using-seq-tp4633739.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Thanks everyone... Seems that I will have to use round before seq to make sure everything has the correct precision. -- View this message in context: http://r.789695.n4.nabble.com/Inconsistency-using-seq-tp4633739p4633750.html Sent from the R help mailing list archive at Nabble.com.
actually, you should build your sequences with integers and scale those to get floating point sequences. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. hamoreno <hamoreno at asu.edu> wrote:>Thanks everyone... Seems that I will have to use round before seq to >make >sure everything has the correct precision. > >-- >View this message in context: >http://r.789695.n4.nabble.com/Inconsistency-using-seq-tp4633739p4633750.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.
On Mon, Jun 18, 2012 at 12:58:13PM -0700, hamoreno wrote:> Hi all, > > Is there any problem of precision when using seq?. For example: > > x<- seq(0,4,0.1) > x[4]=0.3 > > BUT: > > x[4]-0.3=5.551115e-17 > > It means when I use this condition within an if clause, it does not find > values with 0.3 for x[4] as it is not precisely 0.3.Hi. Using round() is reasonably secure. x <- round(seq(0,4,0.1), digits=7) x[4] == 0.3 [1] TRUE None of the compared nunbers is 0.3, but they are both rounded to the closest representable number to 0.3. print(x[4], digits=20) [1] 0.2999999999999999889 print(0.3, digits=20) [1] 0.2999999999999999889 See also http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy for some more hints related to this. Petr Savicky.