当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Java Math cbrt()用法及代码示例

Java Math cbrt() 方法返回指定数字的立方根。

用法:

Math.cbrt(double num)

在这里,cbrt() 是一个静态方法。因此,我们使用类名 Math 访问该方法。

参数:

cbrt() 方法采用单个参数。

  • num- 要计算其立方根的数字

cbrt() 返回值

  • 返回指定数字的立方根
  • 如果指定值为 NaN,则返回 NaN
  • 如果指定的数字为 0,则返回 0

注意: 如果参数是负数-num, 然后cbrt(-num) = -cbrt(num).

示例:Java 数学 cbrt()

class Main {
  public static void main(String[] args) {

    // create a double variable
    double value1 = Double.POSITIVE_INFINITY;
    double value2 = 27.0;
    double value3 = -64;
    double value4 = 0.0;

    // cube root of infinity
    System.out.println(Math.cbrt(value1));  // Infinity

    // cube root of a positive number
    System.out.println(Math.cbrt(value2));  // 3.0

    // cube root of a negative number
    System.out.println(Math.cbrt(value3));  // -4.0

    // cube root of zero
    System.out.println(Math.cbrt(value4));  // 0.0
  }
}

在上面的例子中,我们使用了Math.cbrt()计算立方根的方法无穷,正数,负数, 和.

这里,Double.POSITIVE_INFINITY用于在程序中实现正无穷大。

当我们将整数值传递给cbrt() 方法时,它会自动将int 值转换为double 值。

int a = 125;

Math.cbrt(a);   // returns 5.0

相关用法


注:本文由纯净天空筛选整理自 Java Math cbrt()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。