📜  Java中的 StrictMath cbrt() 方法

📅  最后修改于: 2022-05-13 01:55:48.237000             🧑  作者: Mango

Java中的 StrictMath cbrt() 方法

Java.lang.StrictMath.cbrt()是Java中的一个内置方法,用于返回给定双精度值的立方根。该方法显示了三个特殊结果:

  • 当给定参数为零时,结果为与参数相同符号的零。
  • 当参数是无限的时,结果是具有相同参数符号的无穷大。
  • 当给定参数为 NaN 时,结果为 NaN。

句法:

public static double cbrt(double num)

参数:该方法接受一个参数num ,该参数为 double 类型,需要找到其立方根。
返回值:该方法返回num的立方根。
下面的程序说明了Java.lang.StrictMath.cbrt() 方法:
方案一:

java
// Java program to illustrate the
// java.lang.StrictMath.cbrt()
import java.lang.*;
 
public class Geeks {
 
    public static void main(String[] args)
    {
 
        double val1 = 8.05, val2 = 27, val3 = 0;
 
        // It returns the cube root of a double value
        double cbrtvalue = StrictMath.cbrt(val1);
        System.out.println("Cube root of "+val1+
                                  " = " + cbrtvalue);
 
        cbrtvalue = StrictMath.cbrt(val2);
        System.out.println("Cube root of "+val2+
                                  " = " + cbrtvalue);
 
 
        cbrtvalue = StrictMath.cbrt(val3);
        System.out.println("Cube root of "+val3+
                                  " = " + cbrtvalue);
 
    }
}


java
// Java program to illustrate the
// java.lang.StrictMath.cbrt()
import java.lang.*;
 
public class Geeks {
 
    public static void main(String[] args)
    {
 
        double val1 = -8.05, val2 = 128, val3 = 0;
 
        // It returns the cube root of a double value
        double cbrtvalue = StrictMath.cbrt(val1);
        System.out.println("Cube root of "+val1+
                                  " = " + cbrtvalue);
 
        cbrtvalue = StrictMath.cbrt(val2);
        System.out.println("Cube root of "+val2+
                                  " = " + cbrtvalue);
 
        cbrtvalue = StrictMath.cbrt(val3);
         System.out.println("Cube root of "+val3+
                                  " = " + cbrtvalue);
 
    }
}


输出:
Cube root of 8.05 = 2.0041580161269152
Cube root of 27.0 = 3.0
Cube root of 0.0 = 0.0

方案二:

Java

// Java program to illustrate the
// java.lang.StrictMath.cbrt()
import java.lang.*;
 
public class Geeks {
 
    public static void main(String[] args)
    {
 
        double val1 = -8.05, val2 = 128, val3 = 0;
 
        // It returns the cube root of a double value
        double cbrtvalue = StrictMath.cbrt(val1);
        System.out.println("Cube root of "+val1+
                                  " = " + cbrtvalue);
 
        cbrtvalue = StrictMath.cbrt(val2);
        System.out.println("Cube root of "+val2+
                                  " = " + cbrtvalue);
 
        cbrtvalue = StrictMath.cbrt(val3);
         System.out.println("Cube root of "+val3+
                                  " = " + cbrtvalue);
 
    }
}
输出:
Cube root of -8.05 = -2.0041580161269152
Cube root of 128.0 = 5.039684199579493
Cube root of 0.0 = 0.0