# Inconsistency using seq

9 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Inconsistency using seq

 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() ?
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Inconsistency using seq

 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_003fCheers ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Inconsistency using seq

 In reply to this post by hamoreno 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 <[hidden email]> 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______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Inconsistency using seq

 In reply to this post by hamoreno 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 <[hidden email]> 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. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Inconsistency using seq

 In reply to this post by Sarah Goslee Thanks everyone... Seems that I will have to use round before seq to make sure everything has the correct precision.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Inconsistency using seq

 Nope. round() is not failsafe either. Basically, there is theoretically no way to guarantee immunity to floating point error in conditional comparisons. You need to either switch to integers -- which **are** exactly represented -- or figure out another way. Of course, practically speaking, sufficient rounding will work. It's just that it **can** always break. ... and you know Murphy's first law. -- Bert On Mon, Jun 18, 2012 at 1:54 PM, hamoreno <[hidden email]> 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. > > ______________________________________________ > [hidden email] 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. > -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm        [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Inconsistency using seq

 In reply to this post by hamoreno actually, you should build your sequences with integers and scale those to get floating point sequences. --------------------------------------------------------------------------- Jeff Newmiller                        The     .....       .....  Go Live... DCN:<[hidden email]>        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 <[hidden email]> 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. > >______________________________________________ >[hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Inconsistency using seq

 In reply to this post by Bert Gunter FYI, > isZero <- function(x, neps=1, eps=.Machine\$double.eps, ...) { +  (abs(x) < neps * eps) + } > x <- seq(from=0, to=4, by=0.1) > isZero(x[4]-0.3) [1] TRUE > isZero(x[4]-0.3, neps=1) [1] TRUE > isZero(x[4]-0.3, neps=0.1) [1] FALSE You could also have called isZero() isValueSmallEnoughForWhatINeed(). My \$.02 /Henrik On Mon, Jun 18, 2012 at 2:29 PM, Bert Gunter <[hidden email]> wrote: > Nope. round() is not failsafe either. Basically, there is theoretically no > way to guarantee immunity to floating point error in conditional > comparisons. You need to either switch to integers -- which **are** exactly > represented -- or figure out another way. > > Of course, practically speaking, sufficient rounding will work. It's just > that it **can** always break. ... and you know Murphy's first law. > > -- Bert > > On Mon, Jun 18, 2012 at 1:54 PM, hamoreno <[hidden email]> 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. >> >> ______________________________________________ >> [hidden email] 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. >> > > > > -- > > Bert Gunter > Genentech Nonclinical Biostatistics > > Internal Contact Info: > Phone: 467-7374 > Website: > http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm> >        [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.