[R] Integer bit size and the modulus operator

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jan 31 08:34:35 CET 2006


On Mon, 30 Jan 2006, Peter Dalgaard wrote:

> Ionut Florescu <ifloresc at stevens.edu> writes:
>
>> Actually it does that in my 2.2.1 version as well:
>>
>> > options(digits=20)
>> > 8^(1:20)
>>  [1] 8.0000000000000000e+00 6.4000000000000004e+01 5.1200000000000001e+02
>>  [4] 4.0960000000000001e+03 3.2768000000000002e+04 2.6214400000000002e+05
>>  [7] 2.0971519999999999e+06 1.6777215999999999e+07 1.3421772800000000e+08
>> [10] 1.0737418240000001e+09 8.5899345920000005e+09 6.8719476736000003e+10
>> [13] 5.4975581388799997e+11 4.3980465111039999e+12 3.5184372088832001e+13
>> [16] 2.8147497671065600e+14 2.2517998136852482e+15 1.8014398509481984e+16
>> [19] 1.4411518807585588e+17 1.1529215046068471e+18
>>
>
> Hmm, then it is a platform dependency and my memory playing tricks on
> me... The thing that got fixed was log2(8), perhaps?

It is platform dependent, as it basically uses the system's pow() 
function.  That almost certainly does not use logs.

However, the results above are the result of printing digits that do not 
exist, and reflect the platforms printf() function.  They look like the 
results on Windows, where e.g.

> 8^7 - 2097152
[1] 0
> 8^(1:20) %% 1
  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Warning messages:
1: probable complete loss of accuracy in modulus
2: probable complete loss of accuracy in modulus
3: probable complete loss of accuracy in modulus

despite the discrepancies shown in the printout.

BTW, pow() on Windows was replaced a while back and is the same code as
glibc on ix86 Linux.

So two morals:

1) Do not ignore warnings
2) Distinguish between results and their printed representation.

-- 
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-help mailing list