Java教程

Java Math cbrt()

Java Math cbrt()

Java Math cbrt() 方法返回指定数字的立方根。
cbrt() 方法的语法是:
Math.cbrt(double num)
这里, cbrt() 是一个静态方法。因此,我们使用类名访问该方法, Math

cbrt() 参数

cbrt() 方法接受一个参数。
num-要计算其立方根的数字

cbrt() 返回值

返回指定数字的立方根 如果指定的值为 NaN,则返回 NaN 如果指定的数字为 0,则返回 0
注意: 如果参数是负数-num,则cbrt(-num) =-cbrt(num)。

示例: Java Math 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.pow() Java Math.sqrt()
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4