use*_*764 9 precision largenumber r
我想改变的精度在R的计算.例如,我想计算x^6与x = c(-2.5e+59, -5.6e+60).为了计算它我应该改变R的精度,否则结果是Inf,我不知道该怎么做.
正如Livius在他的评论中指出的那样,这是R(实际上是大多数编程语言)的问题,数字用二进制表示.
要使用极大/小的浮点数,您可以使用该Rmpfr库:
install.packages("Rmpfr")
library("Rmpfr")
x <- c(-2.5e+59, -5.6e+60)
y <- mpfr(x, 6) # the second number is how many decimal places you want
y^6
# 2 'mpfr' numbers of precision 6 bits
# [1] 2.50e356 3.14e364
Run Code Online (Sandbox Code Playgroud)
要使用比R更大的数字可以处理(例如exp(1800))你可以使用"Brobdingnag"包:
install.packages("Brobdingnag")
library("Brobdingnag")
## An example of a single number too large for R:
10^1000.7
# [1] Inf
## Now using the Brobdingnag package:
10^as.brob(1000.7)
# [1] +exp(2304.2)
Run Code Online (Sandbox Code Playgroud)