[Rd] bug in modulus operator %% (PR#7852)
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu May 12 17:09:11 CEST 2005
On Thu, 12 May 2005, Kjetil Brinchmann Halvorsen wrote:
> Prof Brian Ripley wrote:
>
>> I've now found a Windows system that does this. This is also Windows XP,
>> fully patched, and with the same rw2010. So it may be chip-specific: the
>> one that works is a P4 and the one that does not is a latest Pentium M.
>>
>> I am not sure that the guarantee on the help page has been supported for a
>> while. I've altered the code so it is more likely to be.
>>
>> BTW,
>>
>>> options(digits=20)
>>> 1 %% 0.001
>>
>> [1] 0.0009999999999999792
>>
>> shows why the original is not a bug in R.
>>
> Why is that not a bug in R? On my machine (windows XP, rw2010 from CRAN) I
Because 0 <= y %% x < y, so that adding any multiple of 0.001 takes it out
of range.
> get:
>
> test <- function(x, y) (x %% y) + y * ( x %/% y ) # should be x
>> test(1, 0.001)
> [1] 1.001
>> test(1, 0.1)
> [1] 1.1
>> test(1, 1)
> [1] 1
>> test(1, 0.01)
> [1] 1.01
>
> and this differences (well, not the third one) cannot be said to be rounding
> error.
I think they can. The problem is that x %/% y is affected by rounding
error in the representation of 0.01, as in
> 1 %/% 0.01
[1] 99
on my machine. That is due to rounding error.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-devel
mailing list