Java cbrt() 方法示例

java 8object oriented programmingprogramming

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

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

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

  • 如果参数为零,则结果为零,符号与参数相同。

示例

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

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

相关文章