Skip to content

bug in modulus operator %% (PR#7852)

1 message · McGehee, Robert

#
Yes, but from ?"%%":
"It is guaranteed that 'x =3D=3D (x %% y) + y * (x %/% y)' (up to =
rounding
error) ..."

(R 2.1.0)
[1] 0.2
[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@nessie.mcc.ac.uk [mailto:Ted.Harding@nessie.mcc.ac.uk]

Sent: Wednesday, May 11, 2005 3:25 PM
To: Peter Dalgaard
Cc: R-bugs@biostat.ku.dk; kjetil@acelerate.com;
r-devel@stat.math.ethz.ch
Subject: Re: [Rd] bug in modulus operator %% (PR#7852)
On 11-May-05 Peter Dalgaard wrote:
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@nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 11-May-05                                       Time: 20:13:22
------------------------------ XFMail ------------------------------

______________________________________________
R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel