rprecisionmantissa

How to create vector (10^16 + 1, 10^16 + 2, ... , 10^16 + 1000) using R?


I would like to create the following vector (10^16 + 1, 10^16 + 2, ... , 10^16 + 1000). But if I try it like this:

daten <- rep(1e+16,1000)
daten + 1:1000

It gives me just an approximation:

[1] 10000000000000000 10000000000000002 10000000000000004 10000000000000004
[5] 10000000000000004 10000000000000006 10000000000000008 10000000000000008
[9] 10000000000000008 10000000000000010 10000000000000012 10000000000000012
[13] 10000000000000012 10000000000000014 10000000000000016 10000000000000016 
 ......

How can I get R to calculate more precise ? Thank you


Solution

  • The Rmpfr package allows you as much precision as you need. Create numbers using the mpfr function, specifying the value + the number of bits of precision you need.

    library(Rmpfr)
    daten <- mpfr(1:1000, 120)
    daten <- daten + mpfr(1e16, 120)
    print(daten[1:5])
    

    This yields the following:

    5 'mpfr' numbers of precision  120   bits 
    [1] 10000000000000001 10000000000000002 10000000000000003 10000000000000004 10000000000000005