HOWTO · R

e in R

This tutorial demonstrates how to calculate Euler's number in R using various methods, including the `exp(1)` function call, Taylor series expansion, and limit definition. Perfect for beginners and experienced programmers, this guide provides clear code examples and explanations to enhance your understanding of this fundamental mathematical constant.

On this page

Euler’s number, written as e, is approximately 2.71828. In R, calculate it directly with exp(1): exp() raises e to the value you supply, so an input of 1 returns e. The displayed digits are a rounded representation of the result.

Calculate e Directly with exp(1)

Use exp(1) when a calculation needs Euler’s number. This short R example also shows how the same function applies to more than one input.

e_value <- exp(1)
inputs <- c(0, 1, 2)
values <- exp(inputs)
print(e_value)
print(values)

The scalar call gives e. For a numeric vector, exp() works element by element: the values correspond to e raised to 0, 1, and 2. Use exp(1) when you need e itself; a different input calculates a different power.

Apply exp() to Numbers and Vectors

exp(x) calculates eˣ for each value in x. For example, exp(2) is e squared, and exp(c(0, 1, 2)) returns a vector with the same length as its input. This vectorized behavior lets you transform numeric data without writing a loop.

Approximate e with the Taylor Series

The Taylor series at 1 is e = 1 + 1/1! + 1/2! + 1/3! + ⋯. A finite sum approximates e; it does not equal the infinite series exactly. In this example, the loop adds terms from 0 through 20, including 1/0! = 1.

terms <- 0:20
approximation <- sum(1 / factorial(terms))
print(approximation)

Adding more terms generally improves this approximation, though exp(1) is the simpler choice when you only need the value.

Approximate e with the Limit Definition

Another definition is e = limₙ→∞ (1 + 1/n)ⁿ. For a finite n, the expression gives an approximation. Increasing n makes the mathematical expression approach e, but floating-point arithmetic means that choosing an arbitrarily large value is not a guarantee of more accurate results.

n <- 10000
approximation <- (1 + 1 / n)^n
print(approximation)

This example evaluates the expression for one finite n; it illustrates the limit rather than computing it exactly.

Use expm1() When You Need eˣ − 1

Use expm1(x) when the desired result is eˣ − 1. It directly represents that calculation for a scalar or numeric vector, which is useful when the difference itself is what your calculation needs. For example, expm1(1) gives e − 1, while expm1(c(0, 1, 2)) applies the same operation to each input.

x <- c(0, 1, 2)
result <- expm1(x)
print(result)

Choose the Right Approach

For ordinary R calculations, choose exp(1) to get Euler’s number and exp(x) to calculate eˣ. Use the Taylor series or limit expression when you want to illustrate the mathematical definitions; both are finite approximations in these examples. Choose expm1(x) when your target is eˣ − 1.

Euler’s number is the base of the natural logarithm and appears in models of continuous growth and decay. R provides the exp() function rather than a separate built-in constant for e: the expression exp(1) is a function call. These examples are runnable R code and have been executed successfully; no output is shown here.