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


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


java.lang.StrictMath.cbrt()是Java中的内置方法,用于返回给定double值的多维数据集根。该方法显示三个特殊结果:

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

用法:

public static double cbrt(double num)

参数:此方法接受一个参数num,该参数为double类型,需要找到其立方根。


返回值:该方法返回num的立方根。

下面的程序演示了java.lang.StrictMath.cbrt()方法:
示例1:

// Java praogram 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); 
  
    } 
}
输出:
Cube root of 8.05 = 2.0041580161269152
Cube root of 27.0 = 3.0
Cube root of 0.0 = 0.0

示例2:

// Java praogram 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


相关用法


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