[R] a very simple question

Petr Savicky savicky at cs.cas.cz
Mon Mar 19 16:30:27 CET 2012


On Mon, Mar 19, 2012 at 12:47:12PM +0000, Dajiang Liu wrote:
> 
> Thanks a lot for the clarification. I just find it very bizarre that if you run a=0.1*(1:9);which(a==0.4) 
> it returns the right answer. Anyway, I will pay attention next time. Thanks a lot. 

Hi.

Yes, these things are bizarre sometimes. Compare

  print(0.1, digits=20)   # [1] 0.10000000000000000555
  print(4*0.1, digits=20) # [1] 0.4000000000000000222
  print(0.4, digits=20)   # [1] 0.4000000000000000222

Equality of the last two is the reason for

  which(0.1*(1:9) == 0.4)

  [1] 4

while for 0.3, we get

  print(3*0.1, digits=20) # [1] 0.30000000000000004441
  print(0.3, digits=20)   # [1] 0.2999999999999999889

See

  http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy

for further hints.

Petr Savicky.



More information about the R-help mailing list