R: approximating `e = exp(1)` using `(1 + 1 / n) ^ n` gives absurd result when `n` is large

前端 未结 2 2033
陌清茗
陌清茗 2021-01-14 06:16

So, I was just playing around with manually calculating the value of e in R and I noticed something that was a bit disturbing to me.

The value of

2条回答
  •  广开言路
    2021-01-14 07:03

    You might also try the Taylor series approximation to exp(1), namely

    e^x = \sum_{k = 0}{\infty} x^k / k!
    

    Thus we can approximate e = e^1 by truncating this sum; in R:

    sprintf('%.20f', exp(1))
    # [1] "2.71828182845904509080"
    sprintf('%.20f', sum(1/factorial(0:10)))
    # [1] "2.71828180114638451315"
    sprintf('%.20f', sum(1/factorial(0:100)))
    # [1] "2.71828182845904509080"
    

提交回复
热议问题