Skip to content
Prev 312542 / 398506 Next

protentially serious R error

[See in-line below]
On 30-Nov-2012 21:05:23 liuxf wrote:
The key to this case can be seen in:

  20*0.2 - 4
  # [1] 0

  20*(1-0.8)-4
  # [1] -8.881784e-16

  0.2 - (1-0.8)
  # [1] 5.551115e-17

So you have fallen victim to the fact that, in general, floating-point
arithmetic is not exact (and this is not a "feature" only of R, but
of other packages that use standard floating-point CPU arithmetic.
This is effectovely the same issue:

  test1 <- runif(10,0,1)
  test1
  # [1] 0.3868379 0.1587814 0.8140483 0.7796691 0.5357628 0.2431110
  # 0.1782747 0.3906829 0.5262615 0.7440143
  test2 <- NULL
  for(i in seq(0.01,1,length=100)){
    test2[i*100] <- sum(test1<i) 
  }
  test2
  #   [1]  0  0  0  0  0  0 *NA*   0  0  0  0  0  0  0  0  1  1  2  2
  #  2  2  2  2  2  3  3  3  3  3  3  3  3  3  3  3  3  3  3  4  5  5
  #  5  5  5  5  5  5  5  5  5  5  5  6  7  7  7  7  7  7  7  7  7  7
  #  7  7  7  7  7  7  7  7  7  7  7  8  8  8  9  9  9  9 10 10 10 10
  # 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10

  S <- seq(0.01,1,length=100))
  100*S[7] - 7
  # [1] -8.881784e-16

  which(100*S < (1:100))
  # [1] 7

Why not simply use expressions which evaluate to exact integers?
There is no obvious reason in the examples given to do otherwise.

But, if you must adopt your approach, then consider:

  round(100*S)

  100*S - (1:100)
  #  [1]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
  #  [5]  0.000000e+00  8.881784e-16 -8.881784e-16  0.000000e+00
  #  [9]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
  # [13]  0.000000e+00  1.776357e-15  1.776357e-15  0.000000e+00
  # [17]  0.000000e+00  3.552714e-15  0.000000e+00  0.000000e+00
  # [etc .... ]

  round(100*S) - (1:100)
  #  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  # [26] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  # [51] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  # [76] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Hoping this helps,
Ted.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 30-Nov-2012  Time: 23:26:41
This message was sent by XFMail