Skip to content

Problem with long number (from character to numeric class)

9 messages · PtitBleu, Ben Bolker, Marc Schwartz +4 more

#
Hello,

I have a text file with one column containing long number but stored as
string.
I download the file with read.table (and colClass) and the first row of this
column is :

"095842087016731010"

As I need to make some calculations with these numbers, I tried to convert
them using as.numeric.
But then I get
as.numeric("095842087016731010")
95842087016731008

I understand the disappearing of the first 0 but I don't understand the
substitution of the last 0 by 8.

Is there a way to avoid that ?
Thanks in advance,
Ptit Bleu.
#
PtitBleu <ptit_bleu <at> yahoo.fr> writes:
I think you are running into the limitations of double precision
values.  Take a look at the inverse of your value and compare it
to the "eps" values in .Machine.  Then see the R wiki (search for
"precision") for alternatives for extended precision calculations.

  Ben Bolker
#
On Feb 9, 2011, at 6:15 AM, PtitBleu wrote:

            
I get:
[1] 9.584209e+16

# See ?as.character
[1] "95842087016731008"


I suspect you would need to look at using one of the R packages that support arbitrary/infinite precision representations if you need to deal with very large integers. Perhaps look at gmp and Rmpfr.

You are hitting R's limit of 15 significant digits, resulting in the loss of precision in the internal representation of the number, which is in turn, presented in the resultant coercion.

HTH,

Marc Schwartz
#
FAQ 7.31

For 'numerics' there is a limit of about 15 digits of accuracy in the number.
On Wed, Feb 9, 2011 at 7:15 AM, PtitBleu <ptit_bleu at yahoo.fr> wrote:

  
    
#
On Feb 9, 2011, at 7:15 AM, PtitBleu wrote:

            
Your number is many powers of ten above the maximum possible integer  
value 2*10^9. So you are seeing the floating point number that  
resulted from coercion, and R does not have arbitrary precision  
arithmetic in its base repertoire.
There is a package that handles bignums, Brobdingnag.
. And there is interfaces to the symbolic algebra system, Yacas.  
But.... Do you really need arbitrary precision?
David Winsemius, MD
West Hartford, CT
#
Thanks for all your answers. I didn't know this limit.

I finally found another way to go around this problem: luckily the 4 first
figures are always the same for all these numbers.
I created a new column with as.numeric(substr(df$BigNumber,5,18)).
Numbers are now 14-figures long and it is now ok.

Have a nice end of day,
Ptit Bleu.
#
What kind of processing do you need to do with these numbers?  There may be better ways to handle the problem.

Dan

Daniel Nordlund
Bothell, WA USA
#
On Wed, Feb 09, 2011 at 07:39:55AM -0800, PtitBleu wrote:
14 digits are OK.

The exact limit is 2^53 as may be seen from the following

  diff(2^53 - 5:0)

  [1] 1 1 1 1 1

  diff(2^53 + 0:5)

  [1] 0 2 2 0 0

Since 2^53 > 8999999999999999, even 16 digit integers may be
used, if the first digit is at most 8.

Petr Savicky.
#
Hello Dan,

I just need to make substractions, comparisons and to use them in a split
command.
Ptit Bleu.
What kind of processing do you need to do with these numbers?  There may be
better ways to handle the problem.

Dan

Daniel Nordlund
Bothell, WA USA