[R] precision in seq

Duncan Murdoch murdoch at stats.uwo.ca
Tue Feb 5 14:40:03 CET 2008


On 2/5/2008 7:21 AM, Eric Elguero wrote:
> thank you to all who answered.
> 
> 
>> 0+0.05+
> + 0.05+0.05+0.05+0.05+0.05+0.05+
> + 0.05+0.05+0.05+0.05+0.05+0.05+
> + 0.05+0.05+0.05+0.05+0.05+0.05 - 0.95
> [1] 3.330669e-16
> 
>> seq(0,1,0.05)[20] - 0.95
> [1] 1.110223e-16
> 
>> 0+19*0.05 - 0.95
> [1] 1.110223e-16
> 
> so this is the way seq calculates. I would have guessed
> that addition was more accurate than multiplication,
> but that is not the case.

I think they have equal accuracy on modern hardware; the problem is that 
you are performing so many additions, and the errors accumulate.

> this one however bothers me:
>> 19/20-0.95
> [1] 0

0.95 is almost certainly calculated as 95/100, so it shouldn't be too 
surprising that 19/20 gives the same answer.  I wouldn't have bet on it, 
though.

> I noticed this problem when I tried to extract rows of a matrix
> according to whether values of some vector where in the set
> (0,0.05,...,0.95,1), with something like x%in%seq(0,1,0.05)
> Now I understand that I should not use this construction
> unless x is of type integer. Would you agree?

I would be a little less specific:  don't use it unless you are sure the 
values on both sides of %in% can be computed exactly.  That's true for 
integers, but it's also true for some floating point values, i.e. those 
that can be represented as fractions with modest powers of 2 in the 
denominator (0.25 = 1/4, 0.375 = 3/8, etc.  "Modest" may be as large as 
52, but I wouldn't recommend pushing it that far:  you may run into 
inaccuracies when reading the numbers.)  And of course, it's also true 
for character and logical values.

Duncan Murdoch



More information about the R-help mailing list