[Rd] bug in modulus operator %% (PR#7852)
Robert.McGehee at geodecapital.com
Robert.McGehee at geodecapital.com
Wed May 11 22:07:36 CEST 2005
Yes, but from ?"%%":
"It is guaranteed that 'x =3D=3D (x %% y) + y * (x %/% y)' (up to =
rounding
error) ..."
(R 2.1.0)
> x <- 1
> y <- 0.2
> x %% y
[1] 0.2
> (x %% y) + y * (x %/% y)
[1] 1.2
Certainly 1 does not equal 1.2 as the documentation would suggest, and
these seem like large enough numbers to not be effected by rounding
errors or lack of precision.
-----Original Message-----
From: Ted.Harding at nessie.mcc.ac.uk [mailto:Ted.Harding at nessie.mcc.ac.uk]
Sent: Wednesday, May 11, 2005 3:25 PM
To: Peter Dalgaard
Cc: R-bugs at biostat.ku.dk; kjetil at acelerate.com;
r-devel at stat.math.ethz.ch
Subject: Re: [Rd] bug in modulus operator %% (PR#7852)
On 11-May-05 Peter Dalgaard wrote:
> kjetil at acelerate.com writes:
>=20
>> The following can't be right,
>> first rw2010:
>>=20
>> > 1 %% 0.001
>> [1] 0.001
>>=20
>> Then rw2001:
>>=20
>> > 1 %% 0.001
>> [1] -2.081668e-17
>> >
>>=20
>> and the last seems about right. =20
>=20
> A negative remainder? I don't think so. Presumably the result comes
> from
>=20
> o %% now warns if its accuracy is likely to be affected by lack
> of precision (as in 1e18 %% 11, the unrealistic expectation of
> PR#7409), and tries harder to return a value in range when it
> is.
>=20
> So, not a bug.
Agreed! One should always be aware of such fuzzy edges in any case
where the mathematical result depends on mathematically exact
representation, as here.
In such cases what I often do is on the lines of
((1000*x)%%(1000*y))/1000
Using R-2.1.0beta, first of all I get the same as Kjetil:
1 %% 0.001
## [1] 0.001
Secondly I get
(1000*1)%%(1000*0.001)
## [1] 0
Although this trick does not guarantee the exact result, it
makes it more likely; and also, any little inexactness will
now show up more clearly since in the form
((BIG*x) %% (BIG*y))/BIG
the possible discrepancy before division by BIG is at most y
in magnitude. While the properties of this trick are not
entirely transparent, one can for instance compare the result
of using it with the result of not using it, and then come
to a decision as to which to use.
A further example on the same lines is
1 %% (0.00001)
# [1] 1e-05
(Similar to Kjetil's first example), while
((1000*1) %% (1000*0.00001))/1000
## [1] -2.081668e-17
Any comments would be interesting!
Best wishes,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 11-May-05 Time: 20:13:22
------------------------------ XFMail ------------------------------
______________________________________________
R-devel at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list