Skip to content

The "less than" (<) operator doesnt seem to perform as expected

5 messages · Jonas Hal, Sarah Goslee, William Dunlap +2 more

#
This is R FAQ 7.31, about machine representation of
floating point numbers.
[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:

  
    
#
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
#
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:
#
On Thu, Feb 02, 2012 at 10:00:58AM +0000, Jonas Hal wrote:
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.