Jonas Hal
2012-Feb-02 10:00 UTC
[R] The "less than" (<) operator doesnt seem to perform as expected
The example here puzzles me. It seems like the < operator doesn't work as expected.> l <- 0.6 > u <- seq(0.4, 0.7, 0.1) > u[1] 0.4 0.5 0.6 0.7> mygrid <- expand.grid("l" = l, "u" = u) > mygridl u 1 0.6 0.4 2 0.6 0.5 3 0.6 0.6 4 0.6 0.7> mygridcollapsed <- mygrid[mygrid$l < mygrid$u, ] > mygridcollapsedl u 3 0.6 0.6 4 0.6 0.7 In this little example I expect 'mygridcollapsed' only to return row 4 and for it to return row 3 seems wrong. The strange thing is it seems to work if I start the u-sequence at 0.5.> l <- 0.6 > u <- seq(0.5, 0.7, 0.1) > u[1] 0.5 0.6 0.7> mygrid <- expand.grid("l" = l, "u" = u) > mygridl u 1 0.6 0.5 2 0.6 0.6 3 0.6 0.7> mygridcollapsed <- mygrid[mygrid$l < mygrid$u, ] > mygridcollapsedl u 3 0.6 0.7 Maybe I'm missing something... Best wishes Jonas Hal _________________________________________________________________________________________________________________________________________ BRFkredit sender e-mails og vedhaeftede dokumenter i ikke-krypteret form. Hvis du ikke ?nsker at modtage oplysninger fra BRFkredit pr. e-mail, beder vi dig meddele os det via brev eller e-mail. Denne e-mail kan indeholde fortrolig information. Hvis du modtager e-mailen ved en fejl, beder vi dig informere os om det hurtigst muligt. Samtidig beder vi dig slette e-mailen uden at videresende eller kopiere indholdet. _________________________________________________________________________________________________________________________________________ [[alternative HTML version deleted]]
Sarah Goslee
2012-Feb-02 15:54 UTC
[R] The "less than" (<) operator doesnt seem to perform as expected
This is R FAQ 7.31, about machine representation of floating point numbers.> mygrid$u[3] - mygrid$l[3][1] 1.110223e-16 So mygrid$l[3] < mygrid$u[3] is true, though the difference is very, very small and due solely to the limitations of computers. Sarah On Thu, Feb 2, 2012 at 5:00 AM, Jonas Hal <JOH at brf.dk> wrote:> The example here puzzles me. It seems like the < operator doesn't work as expected. > >> l <- 0.6 >> u <- seq(0.4, 0.7, 0.1) >> u > [1] 0.4 0.5 0.6 0.7 >> mygrid <- expand.grid("l" = l, "u" = u) >> mygrid > ? ?l ? u > 1 0.6 0.4 > 2 0.6 0.5 > 3 0.6 0.6 > 4 0.6 0.7 >> mygridcollapsed <- mygrid[mygrid$l < mygrid$u, ] >> mygridcollapsed > ? ?l ? u > 3 0.6 0.6 > 4 0.6 0.7 > > In this little example I expect 'mygridcollapsed' only to return row 4 and for it to return row 3 seems wrong. The strange thing is it seems to work if I start the u-sequence at 0.5. > >> l <- 0.6 >> u <- seq(0.5, 0.7, 0.1) >> u > [1] 0.5 0.6 0.7 >> mygrid <- expand.grid("l" = l, "u" = u) >> mygrid > ? ?l ? u > 1 0.6 0.5 > 2 0.6 0.6 > 3 0.6 0.7 >> mygridcollapsed <- mygrid[mygrid$l < mygrid$u, ] >> mygridcollapsed > ? ?l ? u > 3 0.6 0.7 > > Maybe I'm missing something... > > Best wishes > Jonas Hal > >-- Sarah Goslee http://www.functionaldiversity.org
William Dunlap
2012-Feb-02 15:59 UTC
[R] The "less than" (<) operator doesnt seem to perform as expected
You need to back up a bit to see the root cause of the problem, which is that seq()'s calculations necessarily involve some roundoff error (since it works with 52 binary digits of precision): > u <- seq(from=0.4, to=0.7, by=0.1) > u - c(0.4, 0.5, 0.6, 0.7) [1] 0.000000e+00 0.000000e+00 1.110223e-16 0.000000e+00 > u - (4:7) * 0.1 [1] 0.000000e+00 0.000000e+00 0.000000e+00 -1.110223e-16 > u - (4:7) / 10 [1] 0.000000e+00 0.000000e+00 1.110223e-16 0.000000e+00 > u - cumsum(c(0.4, 0.1, 0.1, 0.1)) [1] 0.000000e+00 0.000000e+00 0.000000e+00 -1.110223e-16 I find the easiest way around this sort of problem is to use integer sequences (use them as subscripts into your real sequence and do the tests on the subscripts). Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jonas Hal > Sent: Thursday, February 02, 2012 2:01 AM > To: r-help at r-project.org > Subject: [R] The "less than" (<) operator doesnt seem to perform as expected > > The example here puzzles me. It seems like the < operator doesn't work as expected. > > > l <- 0.6 > > u <- seq(0.4, 0.7, 0.1) > > u > [1] 0.4 0.5 0.6 0.7 > > mygrid <- expand.grid("l" = l, "u" = u) > > mygrid > l u > 1 0.6 0.4 > 2 0.6 0.5 > 3 0.6 0.6 > 4 0.6 0.7 > > mygridcollapsed <- mygrid[mygrid$l < mygrid$u, ] > > mygridcollapsed > l u > 3 0.6 0.6 > 4 0.6 0.7 > > In this little example I expect 'mygridcollapsed' only to return row 4 and for it to return row 3 > seems wrong. The strange thing is it seems to work if I start the u-sequence at 0.5. > > > l <- 0.6 > > u <- seq(0.5, 0.7, 0.1) > > u > [1] 0.5 0.6 0.7 > > mygrid <- expand.grid("l" = l, "u" = u) > > mygrid > l u > 1 0.6 0.5 > 2 0.6 0.6 > 3 0.6 0.7 > > mygridcollapsed <- mygrid[mygrid$l < mygrid$u, ] > > mygridcollapsed > l u > 3 0.6 0.7 > > Maybe I'm missing something... > > Best wishes > Jonas Hal > > > ______________________________________________________________________________________________________ > ___________________________________ > BRFkredit sender e-mails og vedhaeftede dokumenter i ikke-krypteret form. Hvis du ikke ?nsker at > modtage oplysninger fra BRFkredit pr. e-mail, beder vi dig meddele os det via brev eller e-mail. Denne > e-mail kan indeholde fortrolig information. Hvis du modtager e-mailen ved en fejl, beder vi dig > informere os om det hurtigst muligt. Samtidig beder vi dig slette e-mailen uden at videresende eller > kopiere indholdet. > ______________________________________________________________________________________________________ > ___________________________________ > > [[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.
R. Michael Weylandt
2012-Feb-02 16:03 UTC
[R] The "less than" (<) operator doesnt seem to perform as expected
It's likely an infelicity of floating point representations (R FAQ 7.31) but admittedly, not a case I would have expected to present itself. If you want it to work out as expected, try this: l <- 0.6 u <- seq(0.4, 0.7, 0.1) l.int <- (6L) / 10 u.int <- seq(4, 7) / 10 l < u l.int < u.int Michael On Thu, Feb 2, 2012 at 5:00 AM, Jonas Hal <JOH at brf.dk> wrote:> The example here puzzles me. It seems like the < operator doesn't work as expected. > >> l <- 0.6 >> u <- seq(0.4, 0.7, 0.1) >> u > [1] 0.4 0.5 0.6 0.7 >> mygrid <- expand.grid("l" = l, "u" = u) >> mygrid > ? ?l ? u > 1 0.6 0.4 > 2 0.6 0.5 > 3 0.6 0.6 > 4 0.6 0.7 >> mygridcollapsed <- mygrid[mygrid$l < mygrid$u, ] >> mygridcollapsed > ? ?l ? u > 3 0.6 0.6 > 4 0.6 0.7 > > In this little example I expect 'mygridcollapsed' only to return row 4 and for it to return row 3 seems wrong. The strange thing is it seems to work if I start the u-sequence at 0.5. > >> l <- 0.6 >> u <- seq(0.5, 0.7, 0.1) >> u > [1] 0.5 0.6 0.7 >> mygrid <- expand.grid("l" = l, "u" = u) >> mygrid > ? ?l ? u > 1 0.6 0.5 > 2 0.6 0.6 > 3 0.6 0.7 >> mygridcollapsed <- mygrid[mygrid$l < mygrid$u, ] >> mygridcollapsed > ? ?l ? u > 3 0.6 0.7 > > Maybe I'm missing something... > > Best wishes > Jonas Hal > > > _________________________________________________________________________________________________________________________________________ > BRFkredit sender e-mails og vedhaeftede dokumenter i ikke-krypteret form. Hvis du ikke ?nsker at modtage oplysninger fra BRFkredit pr. e-mail, beder vi dig meddele os det via brev eller e-mail. Denne e-mail kan indeholde fortrolig information. Hvis du modtager e-mailen ved en fejl, beder vi dig informere os om det hurtigst muligt. Samtidig beder vi dig slette e-mailen uden at videresende eller kopiere indholdet. > _________________________________________________________________________________________________________________________________________ > > ? ? ? ?[[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.
Petr Savicky
2012-Feb-02 18:21 UTC
[R] The "less than" (<) operator doesnt seem to perform as expected
On Thu, Feb 02, 2012 at 10:00:58AM +0000, Jonas Hal wrote:> The example here puzzles me. It seems like the < operator doesn't work as expected. > > > l <- 0.6 > > u <- seq(0.4, 0.7, 0.1) > > u > [1] 0.4 0.5 0.6 0.7 > > mygrid <- expand.grid("l" = l, "u" = u) > > mygrid > l u > 1 0.6 0.4 > 2 0.6 0.5 > 3 0.6 0.6 > 4 0.6 0.7 > > mygridcollapsed <- mygrid[mygrid$l < mygrid$u, ] > > mygridcollapsed > l u > 3 0.6 0.6 > 4 0.6 0.7 > > In this little example I expect 'mygridcollapsed' only to return row 4 and for it to return row 3 seems wrong. The strange thing is it seems to work if I start the u-sequence at 0.5.Hi. As others pointed out, the problem is in different rounding error of 0.6 and seq(0.4, 0.7, 0.1)[3]. Try print(0.6, digits=20) [1] 0.5999999999999999778 print(seq(0.4, 0.7, 0.1)[3], digits=20) [1] 0.60000000000000008882 Use round(, digits=1) to force the same rounding in seq(0.4, 0.7, 0.1) and in c(0.4, 0.5, 0.6, 0.7) round(seq(0.4, 0.7, 0.1), digits=1) == c(0.4, 0.5, 0.6, 0.7) Hope this helps. Petr Savicky.