Java - Math exp() Method



Description

The Java Math exp(double a) returns Euler's number e raised to the power of a double value. Special cases:

  • If the argument is NaN, the result is NaN.

  • If the argument is positive infinity, then the result is positive infinity.

  • If the argument is negative infinity, then the result is positive zero.

The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Syntax

public static double exp(double d)

Parameters

Here is the detail of parameters −

  • d − Any primitive data type.

Return Value

  • This method returns the base of the natural logarithms, e, to the power of the argument.

Example 1

In this example, we're showing the usage of Math.exp() method to get the exponent of a double number. We've created two double variables x, y and initialized them with different values. Then using Math.exp() method we're printing the exponent of a double value.

public class Test { 
   public static void main(String args[]) {
      double x = 11.635;
      double y = 2.76;

      System.out.printf("The value of e is %.4f%n", Math.E);
      System.out.printf("exp(%.3f) is %.3f%n", x, Math.exp(x));  
   }
}

This will produce the following result −

Output

The value of e is 2.7183
exp(11.635) is 112983.831

Example 2

In this example, we're showing the usage of Math.exp() method to get the exponent of a float number. We've created two float variables x, y and initialized them with different values. Then using Math.exp() method we're printing the exponent of a float value.

public class Test { 
   public static void main(String args[]) {
      float x = (float)11.635;
      float y = (float)2.76;

      System.out.printf("The value of e is %.4f%n", Math.E);
      System.out.printf("exp(%.3f) is %.3f%n", x, Math.exp(x));  
   }
}

This will produce the following result −

Output

The value of e is 2.7183
exp(11.635) is 112983.831

Example 3

In this example, we're showing the usage of Math.exp() method to get the exponent of zero. We've created a double variable and initialized it with 0.0. Then using Math.exp() method we're printing the exponent of a zero value.

public class Test { 
   public static void main(String args[]) {
      double x = 0.0;

      System.out.printf("The value of e is %.4f%n", Math.E);
      System.out.printf("exp(%.3f) is %.3f%n", x, Math.exp(x));  
   }
}

This will produce the following result −

Output

The value of e is 2.7183
exp(0.000) is 1.000
java_numbers.htm
Advertisements