作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想更改 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 precision **bits** you want - NB: not decimal places!
y^6
# 2 'mpfr' numbers of precision 6 bits
# [1] 2.50e356 3.14e364
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)
关于r - 如何在 R 中处理大数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22466328/
我是一名优秀的程序员,十分优秀!