Java - Math cbrt(double) 方法



描述

Java Math cbrt(double a) 返回雙精度值的立方根。對於正的有限 x,cbrt(-x) == -cbrt(x);也就是說,負值的立方根是該值大小的立方根的負數。特殊情況 -

  • 如果引數是 NaN,則結果是 NaN。

  • 如果引數是無限的,則結果是與引數符號相同的無窮大。

  • 如果引數是零,則結果是與引數符號相同的零。

計算結果必須在精確結果的 1 ulp 之內。

宣告

以下是 java.lang.Math.cbrt() 方法的宣告

public static double cbrt(double a)

引數

a - 一個值。

返回值

此方法返回 a 的立方根。

異常

獲取正數的立方根示例

以下示例演示了 Math cbrt() 方法的使用。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = 125;

      // print the cube root of the number
      System.out.println("Math.cbrt(" + x + ")=" + Math.cbrt(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果 -

Math.cbrt(125.0)=5.0

獲取零的立方根示例

以下示例演示了 Math cbrt() 方法對零值的使用。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = 0;

      // print the cube root of the number
      System.out.println("Math.cbrt(" + x + ")=" + Math.cbrt(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果 -

Math.cbrt(0.0)=0.0

獲取負數的立方根示例

以下示例演示了 Math cbrt() 方法對負數的使用。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = -10;

      // print the cube root of the number
      System.out.println("Math.cbrt(" + x + ")=" + Math.cbrt(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果 -

Math.cbrt(-10.0)=-2.154434690031884
java_lang_math.htm
廣告