Why is the as.numeric function in R not working properly?

I have these two characters, and the "as.numeric" function does not work the same for them. Can someone help me why this is happening?

options (digits = 22)

a = "27"

as.numeric (a)

[1] 27.00000000000000000000

a = "193381411288395777"

as.numeric (a)

[1] 193381411288395776.0000

You can see that in the second case, the last digit is not "7", but "6". In principle, the function "as.numeric" reduces 1 unit from a number in the second case.

Any help is appreciated.

0
source share
2 answers

You need to know the boundaries of the representation of exact numbers. R can tell you that he has:

R> .Machine $double.eps [1] 2.22045e-16 $double.neg.eps [1] 1.11022e-16 $double.xmin [1] 2.22507e-308 $double.xmax [1] 1.79769e+308 $double.base [1] 2 $double.digits [1] 53 $double.rounding [1] 5 $double.guard [1] 0 $double.ulp.digits [1] -52 $double.neg.ulp.digits [1] -53 $double.exponent [1] 11 $double.min.exp [1] -1022 $double.max.exp [1] 1024 $integer.max [1] 2147483647 $sizeof.long [1] 8 $sizeof.longlong [1] 8 $sizeof.longdouble [1] 16 $sizeof.pointer [1] 8 R> 
+2
source

Use int64 package:

 library(int64) > as.int64("193381411288395777") [1] 193381411288395777 
+2
source

Source: https://habr.com/ru/post/986010/


All Articles