Java exp 函数

Java exp 函数是 Math 函数之一,它用于返回 E 的 double 值幂。其中 E 是欧拉数,约等于 2.71828。

Math.exp 在 Java 编程语言中的基本语法如下所示。

static double exp(double number); //Return Type is Double

// In order to use in program: 
Math.exp(double number);

数字:可以是 double 值或有效的数值表达式,表示指数值。

  • 如果数字参数是正数或负数的 double 值,Math.exp 函数将返回输出。
  • 如果数字参数不是数字,Java Math.exp 函数将返回 NaN。
  • 当数字参数为正无穷大时,Math.exp 函数将返回正无穷大作为输出。
  • 如果它是负无穷大,Math.exp 函数将返回正零作为输出。

例如,如果我们指定表达式为 Math.exp(2.00)。这意味着 e² ==> 2.718² ==> 7.38

Java exp 函数示例

在此程序中,我们使用 Math.exp 函数计算正数和负数欧拉数 E 的幂,并显示输出。

package MathFunctions;

public class ExpMethod {
	public static void main(String[] args) {
		double a = Math.exp(10.9666 - 14.9865 + 10.9852);
		System.out.println("Math.Exp Result = = " + a);	

		System.out.println("\nMath.Exp Result of Positive Number = " + Math.exp(4.25));
		System.out.println("Math.Exp Result of Positive Number = " + Math.exp(6.95));
		
		System.out.println("\nMath.Exp Result of Negative Number = " + Math.exp(-2.85));
		System.out.println("Math.Exp Result of Negative Number = " + Math.exp(-10.25));
		
		System.out.println("\nMath.Exp Result = " + Math.exp(1));
		System.out.println("Math.Exp Result = " + Math.exp(-1));
	}
}
exp Function 1

首先,我们声明了一个 Double 类型的变量,并直接对表达式执行了 Math.exp 函数。

double a = Math.exp(10.9666 - 14.9865 + 10.9852);
System.out.println("Math.Exp Result = = " + a);

接下来,我们直接对正 double 值使用了 Math.exp 函数。此处,Math.exp(4.25) 意味着 (2.71828)^4.25

System.out.println("\nMath.Exp Result of Positive Number = " + Math.exp(4.25));
System.out.println("Math.Exp Result of Positive Number = " + Math.exp(6.95));

此处,我们直接对负 double 值使用了该函数。

System.out.println("\nMath.Exp Result of Negative Number = " + Math.exp(-2.85));
System.out.println("Math.Exp Result of Negative Number = " + Math.exp(-10.25));

接下来,我们直接对正数和负数 double 值使用了 Math.exp 函数。

System.out.println("\nMath.Exp Result = " + Math.exp(1));
System.out.println("Math.Exp Result = " + Math.exp(-1));

Java exp 数组示例

在这个 Java 程序中,我们返回了欧拉数 E 的批量数据的幂。这里,我们将声明一个 double 类型的数组,并使用数组元素的 Math.exp 函数。

package MathFunctions;

public class ExpMethodOnArrays {
	public static void main(String[] args) {
		
		double [] myArray = {-1.69, 5.98, 4.21, -3.9999, 6.879, 4.4897};

		for (int i = 0; i < myArray.length; i++) {
			System.out.format("Math.Exp Result of Array Element = %.2f\n", Math.exp(myArray[i]));
		}
	}
}
Math.Exp Result of Array Element = 0.18
Math.Exp Result of Array Element = 395.44
Math.Exp Result of Array Element = 67.36
Math.Exp Result of Array Element = 0.02
Math.Exp Result of Array Element = 971.65
Math.Exp Result of Array Element = 89.09

我们使用 For 循环 来迭代数组。在 exp For 循环中,我们将 i 的值初始化为 0。接下来,编译器将检查条件 (i < myArray.length)。只要条件为真,将执行 for 循环内的语句。

提示:myArray.length 查找 Java 数组的长度。

for (int i = 0; i < myArray.length; i++) {

在这里,我们在 System.out.format 语句中直接使用了 exp Math 函数。这里,编译器将调用 Math.exp 方法 ( static double exp(double number) ) 来查找相应的 exp 值并打印输出。

System.out.format("Math.Exp Result of Array Element = %.2f\n", Math.exp(myArray[i]));

注意:要对单个项使用 Math.exp,请使用:Math.exp(myArray[index_position])

Java exp ArrayList 示例

在这个 Java 程序中,我们将声明一个 double 类型的 ArrayList,并返回列表元素的欧拉数 E 的幂。

package MathFunctions;

import java.util.ArrayList;

public class ExpMethodOnArrayList {
	public static void main(String[] args) {
		
		ArrayList<Double> myList = new ArrayList<Double>(5);
		myList.add(-2.5);
		myList.add(2.5);
		myList.add(4.25);
		myList.add(-4.25);
		myList.add(5.00);
		
		for (double x : myList) {
			System.out.println("Math.Exp Result of ArrayList =  " + Math.exp(x));
		}
	}
}
Math.Exp Result of ArrayList =  0.0820849986238988
Math.Exp Result of ArrayList =  12.182493960703473
Math.Exp Result of ArrayList =  70.10541234668786
Math.Exp Result of ArrayList =  0.014264233908999256
Math.Exp Result of ArrayList =  148.4131591025766

我们使用 For 循环迭代 ArrayList 中的双精度值。

for (double x : myList) {

这里,编译器将调用 ( static double exp(double x) 方法来查找相应的 exp 值并打印输出。

System.out.println("Math.Exp Result of ArrayList =  " + Math.exp(x));