Java cbrt() 方法附帶示例
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) {
// get two double numbers
double x = 125;
double y = 10;
// print the cube roots of three numbers
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) {
// get two double numbers
double x = 0.0;
double y = -343.0;
// print the cube roots of three numbers
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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP