我想用R打印例如1000或2000或15000小数的pi值?
现在我只有六个
> pi
[1] 3.141593
Run Code Online (Sandbox Code Playgroud)
怎么做到这一点?
R可以准确表示多少位数字?基于问题的答案在这里,并在这里和文档,我希望R键好得多做的比我在下面的迷你例子(精度击穿后16位数字):
log(.Machine$double.xmax, 10)
# 308.2547
for (i in 1:30) {
a <- rep(1, i) # make a vector of 1s
b <- paste(a, collapse = '') # turn into string
d <- as.double(b) # turn into double
e <- format(d, scientific = FALSE) # turn into string
print(e)
}
# "1"
# "11"
# "111"
# "1111"
# "11111"
# "111111"
# "1111111"
# "11111111"
# "111111111"
# "1111111111"
# "11111111111"
# "111111111111"
# …Run Code Online (Sandbox Code Playgroud) 所以,我只是在手动计算eR中的值,我发现有些东西对我来说有点令人不安.
e使用R exp()命令的价值......
exp(1)
#[1] 2.718282
Run Code Online (Sandbox Code Playgroud)
现在,我将尝试使用手动计算它 x = 10000
x <- 10000
y <- (1 + (1 / x)) ^ x
y
#[1] 2.718146
Run Code Online (Sandbox Code Playgroud)
不完全,但我们会尝试更接近使用 x = 100000
x <- 100000
y <- (1 + (1 / x)) ^ x
y
#[1] 2.718268
Run Code Online (Sandbox Code Playgroud)
温暖但有点偏......
x <- 1000000
y <- (1 + (1 / x)) ^ x
y
#[1] 2.71828
Run Code Online (Sandbox Code Playgroud)
现在,让我们尝试一个巨大的
x <- 5000000000000000
y <- (1 + (1 / x)) ^ x
y
#[1] 3.035035 …Run Code Online (Sandbox Code Playgroud)