[R] Modular Arithmetic Error?

Marc Schwartz marc_schwartz at me.com
Fri Apr 17 16:45:32 CEST 2009


On Apr 17, 2009, at 9:29 AM, iuhz7j202 at sneakemail.com wrote:

> A few further oddities (no longer having to do with modular  
> arithmetic):
>
>> (0.3 * 3) == 0.9
> [1] FALSE
>> (0.2 * 6) == 1.2
> [1] FALSE
>
> This is bad behavior, right?

No...

 > (0.3 * 3) - 0.9
[1] -1.110223e-16

 > (0.2 * 6) - 1.2
[1] 2.220446e-16


However:

 > all.equal(0.3 * 3, 0.9)
[1] TRUE

 > all.equal(0.2 * 6, 1.2)
[1] TRUE


See ?all.equal


> I feel like I'm going crazy.

Only because you don't understand the etiology of the behavior.

Read:

R FAQ 7.31 Why doesn't R think these numbers are equal?

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

and call us in the morning....  :-)

HTH,

Marc Schwartz




More information about the R-help mailing list