[R] Modulus Problem
Thomas Lumley
tlumley at u.washington.edu
Wed Dec 8 18:44:33 CET 2004
On Wed, 8 Dec 2004, McGehee, Robert wrote:
> R users, I am having a problem with the modulus operator for large
> numbers as follows,
>
> a <- 2
> n <- 561
> ## n is the first Carmichael number, so by Fermat's Little Theorem the
> below should equal zero.
>
> (a^(n-1) - 1) %% n
> [1] 2.193172e+152
> ## Seems that R and Fermat disagree
>
> ## Also,
> 1000000000000000000 %% 11
> [1] -32
>
> This seems like a bug. Should I be avoiding integer math for large
> numbers?
You can find out the largest representable integer from
.Machine$integer.max, and it is probably 2^31-1. Numbers larger than that
are converted to double precision. On the other hand, %% should probably
return an error or NaN if .Machine$double.eps times the first operand is
greater than 1.
-thomas
More information about the R-help
mailing list