Java Math.exp()

Share this article

In Java, Math.exp() is a method that returns e (Euler’s number, approximately 2.718) raised to the power of a given number. This method is part of the java.lang.Math class and is frequently used in exponential growth calculations, scientific formulas, and other areas involving continuous processes.

Example

System.out.println(Math.exp(1));  // 2.718 (which is e^1)
System.out.println(Math.exp(0));  // 1 (because e^0 is always 1)
System.out.println(Math.exp(2));  // 7.389 (which is e^2)

What it does

Math.exp() calculates e raised to the power of a number. This is commonly used in exponential growth models and calculations involving natural processes. The method gives the result of e to the power of the input number.

  • True result: When the input is a positive number, the result is a value greater than 1, as e raised to a positive exponent grows quickly.
  • Zero result: When the input is 0, the result is always 1, because any number raised to the power of 0 equals 1.

Examples

Example 1: Basic usage with positive numbers

System.out.println(Math.exp(3));  // 20.085 (which is e^3)

Passing a positive number like 3 returns e raised to the power of 3, giving you around 20.085. This demonstrates how the function rapidly increases the result for higher positive inputs.

Example 2: Using 0 as input

System.out.println(Math.exp(0));  // 1

If the input is 0, Math.exp() returns 1 because e to the power of 0 is always 1, as is the case with any base raised to the power of zero.

Example 3: Negative exponent

System.out.println(Math.exp(-2));  // 0.135 (which is 1/e^2)

When the input is negative, such as -2, Math.exp() returns a value between 0 and 1, as it calculates 1 / e². This is useful for modeling decay or diminishing returns over time.

Example 4: Using in a formula

double growthRate = 0.05;
double years = 10;
double futureValue = Math.exp(growthRate * years);
System.out.println(futureValue);  // 1.6487 (which shows exponential growth)

You can use Math.exp() in practical scenarios like calculating exponential growth. For example, multiplying a growth rate by time (in years) and passing it to Math.exp() will give you the value showing how much something has grown exponentially.

Here, a 5% growth rate over 10 years results in approximately 1.6487, illustrating how this method can be applied in financial growth models.