Java expm1 函数

Java expm1 函数是 Math 函数之一,用于返回 E 的 double 值次幂再减去 1 的值。其中,E 是欧拉数,约等于 2.71828。

Java 中 Math.expm1 的基本语法如下所示。

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

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

Number:它可以是 double 值或代表指数的有效数字表达式。

  • 如果 number 参数是正数或负数 double 值,Math.expm1 函数将返回输出。
  • 如果 number 参数不是数字,Math.expm1 函数将返回 NaN。
  • 当 number 参数是正无穷时,Java expm1 函数将返回正无穷作为输出。
  • 如果是负无穷,Math.expm1 函数将返回 -1.0 作为输出。

例如,如果我们指定 Math.expm1 (2.00); 意味着 e² – 1 ==> 2.718² – 1 ==> 6.38

Java expm1 函数示例

Math.expm1 函数计算欧拉数 E 的幂并从中减去一。在此 Java 程序中,我们将计算正值和负值的 E 减 1 的幂并显示输出。

package MathFunctions;

public class Expm1Method {
	public static void main(String[] args) {
		double a = Math.expm1(5.12 - 4.65 + 3.21);
		System.out.println("Math.Expm1 Result = = " + a);	

		System.out.println("\nMath.Expm1 Result of Positive Number = " + Math.expm1(10.45));
		System.out.println("Math.Expm1 Result of Positive Number = " + Math.expm1(9.52));
		
		System.out.println("\nMath.Expm1 Result of Negative Number = " + Math.expm1(-8.50));
		System.out.println("Math.Expm1 Result of Negative Number = " + Math.expm1(-4.25));
		
		System.out.println("\nMath.Expm1 Result = " + Math.expm1(1));
		System.out.println("Math.Expm1 Result = " + Math.expm1(-1));
	}
}
math expm1 Function 1

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

double a = Math.expm1(5.12 - 4.65 + 3.21);
System.out.println("Math.Expm1 Result = = " + a);

接下来,我们直接对正 double 值使用了 Math.expm1 函数。这里,Math.expm1(10.45) 意味着 (2.71828)^10.45 – 1

System.out.println("\nMath.Expm1 Result of Positive Number = " + Math.expm1(10.45));
System.out.println("Math.Expm1 Result of Positive Number = " + Math.expm1(9.52));

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

System.out.println("\nMath.Expm1 Result of Negative Number = " + Math.expm1(-8.50));
System.out.println("Math.Expm1 Result of Negative Number = " + Math.expm1(-4.25));

最后,我们直接对正数和负数 double 值都使用了 expm1 函数。

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

Java expm1 对数组的示例

在此程序中,我们将此函数用于数组项。这里,我们将声明一个 double 类型的数组并使用数组元素的 Math.expm1 函数。

public class Expm1MethodOnArrays {
	public static void main(String[] args) {
		
		double [] myArray = {2.35, 4.98, -8.41, -9.5987, 12.38, 14.4897};

		for (int i = 0; i < myArray.length; i++) {
			System.out.format("Math.Expm1 Result of Array Element = %.2f\n", Math.expm1(myArray[i]));
		}
	}
}
Math.Expm1 Result of Array Element = 9.49
Math.Expm1 Result of Array Element = 144.47
Math.Expm1 Result of Array Element = -1.00
Math.Expm1 Result of Array Element = -1.00
Math.Expm1 Result of Array Element = 237992.82
Math.Expm1 Result of Array Element = 1962440.66

我们使用 Java For 循环 来迭代数组。在 expm1 For 循环中,我们将 i 值初始化为 0。接下来,编译器将检查条件(i < myArray.length)。

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

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

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

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

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

Java expm1 函数在 ArrayList 上的示例

在此 程序 中,我们将声明一个 double 类型的 ArrayList 并返回 list 元素的 Math.expm1 函数结果。

package MathFunctions;

import java.util.ArrayList;

public class Expm1MethodOnArrayList {
	public static void main(String[] args) {		
		ArrayList<Double> myList = new ArrayList<Double>(5);
		myList.add(-1.5);
		myList.add(1.5);
		myList.add(2.45);
		myList.add(-2.45);
		myList.add(6.00);
		
		for (double x : myList) {
			System.out.println("Math.Expm1 Result of ArrayList =  " + Math.expm1(x));
		}
	}
}
Math.Expm1 Result of ArrayList =  -0.7768698398515702
Math.Expm1 Result of ArrayList =  3.481689070338065
Math.Expm1 Result of ArrayList =  10.588346719223392
Math.Expm1 Result of ArrayList =  -0.9137064135006295
Math.Expm1 Result of ArrayList =  402.4287934927351

我们使用 For 循环来迭代 ArrayList 中的 double 值

for (double x : myList) {

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

System.out.println("Math.Expm1 Result of ArrayList =  " + Math.expm1(x));