[R] a very simple question

Petr Savicky savicky at cs.cas.cz
Mon Mar 19 09:19:02 CET 2012

On Sun, Mar 18, 2012 at 09:43:54PM +0000, Dajiang Liu wrote:
> Dear All,
> I have a seemingly very simple question, but I just cannot figure out the answer. I attempted to run the following:a=0.1*(1:9);which(a==0.3);it returns integer(0). But obviously, the third element of a is equal to 0.3. 
> I must have missed something. Can someone kindly explain why? Thanks a lot.


A simple way to detect rounding problems is subtracting
the numbers.

  a = 0.1*(1:4)
  a - 0.3

  [1] -2.000000e-01 -1.000000e-01  5.551115e-17  1.000000e-01

Use rounding to avoid it as suggested by others.

Petr Savicky.

More information about the R-help mailing list