[R] erratic behavior of match()?

Duncan Murdoch murdoch at stats.uwo.ca
Thu Apr 19 23:06:24 CEST 2007


On 4/19/2007 4:29 PM, Bernhard Klingenberg wrote:
> Thank you! Is floating point arithmetic also the reason why 
> 
> 1 %% 0.1
> 
> gives the "surprising" answer 0.1 (because 0.1 cannot be written as a 
> fraction with denominator a power of 2, e.g. 1%%0.5 correctly gives 0).
> 
> This seems to go a bit against the statement in the help for '%%', which 
> states "For real arguments, '%%' can be subject to catastrophic loss of 
> accuracy if 'x' is much larger than 'y', and a warning is given if this 
> is detected."

I don't see the contradiction.  The statement is talking about one way 
to get imprecise results; you may have found another.

However, I'm not sure if you can blame %% in your example:  the loss of 
precision probably came from the translation of "0.1" to the internal 
representation.  I think "0.1" ends up a little bit larger than 0.1 
after string conversion and rounding, so 1 %/% 0.1 should give 9, and 1 
%% 0.1 should give something very close to 0.1, as you saw.

Duncan Murdoch



More information about the R-help mailing list