Java cbrt()方法与示例

java.lang.Math.cbrt(double a)返回double值的多维数据集根。对于正有限x,cbrt(-x)== -cbrt(x); 也就是说,负值的立方根就是该值大小的立方根的负值。特殊情况-

  • 如果参数为NaN,则结果为NaN。

  • 如果参数为无穷大,则结果为无穷大,其符号与参数相同。

  • 如果自变量为零,则结果为零,其符号与自变量相同。

示例

以下是cbrt()在Java中实现该方法的示例-

import java.lang.*;
public class Example {
   public static void main(String[] args) {
      //得到两个双数
      double x = 125;
      double y = 10;
      //打印三个数字的立方根
      System.out.println("Math.cbrt(" + x + ")=" + Math.cbrt(x));
      System.out.println("Math.cbrt(" + y + ")=" + Math.cbrt(y));
      System.out.println("Math.cbrt(-27)=" + Math.cbrt(-27));
   }
}

输出结果

Math.cbrt(125.0)=5.0
Math.cbrt(10.0)=2.154434690031884
Math.cbrt(-27)=-3.0

示例

让我们看另一个例子-

import java.lang.*;
public class Example {
   public static void main(String[] args) {
      //得到两个双数
      double x = 0.0;
      double y = -343.0;
      //打印三个数字的立方根
      System.out.println("Math.cbrt(" + x + ")=" + Math.cbrt(x));
      System.out.println("Math.cbrt(" + y + ")=" + Math.cbrt(y));
   }
}

输出结果

Math.cbrt(0.0)=0.0
Math.cbrt(-343.0)=-7.0