📜  Java中的 StrictMath pow() 方法

📅  最后修改于: 2022-05-13 01:55:02.637000             🧑  作者: Mango

Java中的 StrictMath pow() 方法

Java.lang.StrictMath.pow()是StrictMath 类的内置方法,用于查找幂值,即一个参数的值提升到另一个参数的幂。数学上指的是a^b  .它产生了三个特殊的结果:

  • 当一个参数是 NaN 而另一个是非零参数或 NaN 时,该方法返回 NaN。
  • 当第二个参数为 1.0 时,该方法返回与第一个参数相同的值。
  • 当第二个参数为正零或负零时,该方法返回 1.0。

句法:

public static double pow(double num1, double num2)

参数:该方法接受两个参数:

  • num1:这是双精度类型,是指基数。
  • num2:这是双精度类型,指的是指数。

返回值:该方法返回操作num1^num2的值。
例子 :

Input: num1 = 6
       num2 = 4
Output: 1296.0

Input: num1 = 5
       num2 = 2
Output: 25.0

下面的程序说明了Java.lang.StrictMath.pow() 方法:
方案一:

java
// Java program to illustrate the
// java.lang.StrictMath.pow()
import java.lang.*;
 
public class Geeks {
 
    public static void main(String[] args)
    {
 
        double num1 = 12, num2 = 6;
 
        // It returns num1 to the power num2
        double pow_Value = StrictMath.pow(num1, num2);
        System.out.print(num1 + " to the power of " +
                                        num2 + " = ");
        System.out.println(pow_Value);
 
        double pow_Value1 = StrictMath.pow(num1, 0);
        double pow_Value2 = StrictMath.pow(num1, 0);
        System.out.println(num1+" raised to the"+
                        " power 0 = " + pow_Value1);
        System.out.println(num2+" raised to the"+
                        " power 0 = " + pow_Value2);
    }
}


java
// Java program to illustrate the
// java.lang.StrictMath.pow()
import java.lang.*;
 
public class Geeks {
 
    public static void main(String[] args)
    {
 
        double num1 = 4.7, num2 = 2.5;
 
        // It returns num1 to the power num2
        double pow_Value = StrictMath.pow(num1, num2);
        System.out.print(num1 + " to the power of " +
                                        num2 + " = ");
        System.out.println(pow_Value);
    }
}


输出:
12.0 to the power of 6.0 = 2985984.0
12.0 raised to the power 0 = 1.0
6.0 raised to the power 0 = 1.0

方案二:

Java

// Java program to illustrate the
// java.lang.StrictMath.pow()
import java.lang.*;
 
public class Geeks {
 
    public static void main(String[] args)
    {
 
        double num1 = 4.7, num2 = 2.5;
 
        // It returns num1 to the power num2
        double pow_Value = StrictMath.pow(num1, num2);
        System.out.print(num1 + " to the power of " +
                                        num2 + " = ");
        System.out.println(pow_Value);
    }
}
输出:
4.7 to the power of 2.5 = 47.88997880559147