當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Math.pow()用法及代碼示例

java.lang.Math.pow() 用於返回第一個參數的值的第二個參數的冪。 pow() 方法的返回類型是 double。

用法

public static double pow(double a, double b)

參數

a= base 
b= exponent

返回

此方法返回一個值b

  • 如果第二個參數是正零或負零,則此方法將返回 1.0。
  • 如果第二個參數不是數字 (NaN),則此方法將返回 NaN。
  • 如果第二個參數為 1,則此方法將返回與第一個參數相同的結果。

例子1

public class PowExample1
{
    public static void main(String[] args) 
    {
        double x = 5;
        double y = 4;
        //returns 5 power of 4 i.e. 5*5*5*5
        System.out.println(Math.pow(x, y));
    }
}

輸出:

625.0

例子2

public class PowExample2
{
    public static void main(String[] args) 
    {
        double x = 9.0;
        double y = -3;
        //return (9) power of -3
        System.out.println(Math.pow(x, y));
    }
}

輸出:

0.0013717421124828531

例子3

public class PowExample3
{
    public static void main(String[] args) 
    {
        double x = -765;
        double y = 0.7;
        //return NaN
        System.out.println(Math.pow(x, y));
    }
}

輸出:

NaN

示例 4

public class PowExample4
{
    public static void main(String[] args) 
    {
        double x = 27.2;
        double y = 1.0;
        // Second argument is 1 so output is 27.2 
        System.out.println(Math.pow(x, y));
    }
}

輸出:

3.5461138422596736






相關用法


注:本文由純淨天空篩選整理自 Java Math.pow() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。