jeroen / openssl

OpenSSL bindings for R
Other
63 stars 19 forks source link

bignum incorrect conversion #114

Open minemR opened 1 year ago

minemR commented 1 year ago

Using bignum I encountered that portion of numerics (99994999999991:99999999999769) are not converted correctly:

library(openssl)
bignum(99994999999990)
# [b] 99994999999990 # correct
bignum(99994999999991) # + 1
# [b] 100000000000000 # incorrect
bignum(99999999999769) # + 5e+09
# [b] 100000000000000 # incorrect
bignum(99999999999770) # + 1
# [b] 99994999999990 # correct

but, if we use character, conversion is correct:

bignum('99994999999991')
# [b] 99994999999991

Looking at undelaying code we can see that formatC is to be blamed:

formatC(99994999999991, format = "fg")
# [1] "100000000000000"

Maybe this could be fixed replacing formatC with format?

format(99994999999991, scientific = F)
# "99994999999991"
format(99994999999991.11, scientific = F)
# "99994999999991" # should warn?
format(1.13, scientific = F) # "1.13"

Or maybe conversion from double should be turned off and the pitfalls should be described in help?