MangooSaSa
MangooSaSa

Reputation: 3735

precision of double in R

I have to calculate the difference between two long variables in R. Initially, it was stored as text. But when I tried to convert them into numeric or double to calculate the difference, R fails to recognize that the difference is 1.

testVariable1 = as.numeric("233203300000000001")
testVariable2 = as.numeric("233203300000000002")
testVariable2 - testVariable1

Result:

[1] 0

What can I do to solve this issue?

Thanks in advance!

Upvotes: 4

Views: 1080

Answers (2)

Yorgos
Yorgos

Reputation: 30445

library(gmp)

as.bigz("233203300000000002")-as.bigz("233203300000000001")


Big Integer ('bigz') :
[1] 1

Upvotes: 3

nrussell
nrussell

Reputation: 18602

You could try using the bit64 package:

library(bit64)
##
testVariable1 <- as.integer64("233203300000000001")
testVariable2 <- as.integer64("233203300000000002")
##
R> testVariable2 - testVariable1
#integer64
#[1] 1
R> as.numeric(testVariable2 - testVariable1)
#[1] 1

Upvotes: 2

Related Questions