Guava LongMath類的sqrt(long x,RoundingMode模式)方法接受兩個參數,並根據第二個參數指定的舍入模式計算第一個參數的平方根。
用法:
public static long sqrt(long x, RoundingMode mode)
參數:此方法有兩個參數:
- x:是其平方根的long值。
- mode:是要進行舍入的舍入模式。
返回值:此方法返回x的平方根,並根據指定的舍入模式進行舍入。
異常:此方法引發以下參數:
- IllegalArgumentException:如果x <0。
- ArithmeticException:如果mode為RoundingMode.UNNECESSARY且sqrt(x)不是整數。
Enum RoundingMode
枚舉常量 | 描述 |
---|---|
CEILING | 舍入模式向正無窮大舍入。 |
DOWN | 舍入模式向零舍入。 |
FLOOR | 舍入模式向負無窮大舍入。 |
HALF_DOWN | 除非兩個鄰居等距,否則舍入模式將朝“nearest neighbor”舍入。 |
HALF_EVEN | 舍入模式向“nearest neighbor”舍入,除非兩個鄰居都等距,在這種情況下,向偶數鄰居舍入。 |
HALF_UP | 除非兩個鄰居等距,否則舍入模式將朝“nearest neighbor”舍入。 |
UNNECESSARY | 舍入模式可以斷言所請求的操作具有準確的結果,因此不需要舍入。 |
UP | 舍入模式從零舍入。 |
下麵給出了一些示例,以更好地理解實現:
範例1:
// Java code to show implementation of
// sqrt(long x, RoundingMode mode) method
// of Guava's LongMath class
import java.math.RoundingMode;
import com.google.common.math.LongMath;
class GFG {
// Driver code
public static void main(String args[])
{
long x1 = 524;
// Using sqrt(long x, RoundingMode mode)
// method of Guava's LongMath class
// The RoundingMode HALF_EVEN rounds towards
// the nearest neighbor unless both neighbors
// are equidistant, in which case, round towards
// the even neighbor.
long ans1
= LongMath.sqrt(x1,
RoundingMode.HALF_EVEN);
System.out.println("Square root of " + x1
+ " is:" + ans1);
long x2 = 316;
// Using sqrt(long x, RoundingMode mode)
// method of Guava's LongMath class
// The RoundingMode FLOOR rounds towards
// negative infinity.
long ans2
= LongMath.sqrt(x2,
RoundingMode.FLOOR);
System.out.println("Square root of " + x2
+ " is:" + ans2);
}
}
輸出:
Square root of 524 is:23 Square root of 316 is:17
範例2:
// Java code to show implementation of
// sqrt(long x, RoundingMode mode) method
// of Guava's LongMath class
import java.math.RoundingMode;
import com.google.common.math.LongMath;
class GFG {
// Driver code
public static void main(String args[])
{
int x = -65;
try {
// Using sqrt(long x, RoundingMode mode)
// method of Guava's LongMath class
// The RoundingMode HALF_EVEN rounds towards
// the nearest neighbor unless both neighbors
// are equidistant, in which case, round towards
// the even neighbor.
// This should throw "IllegalArgumentException"
// as x < 0
long ans1 = LongMath.sqrt(x,
RoundingMode.HALF_EVEN);
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
java.lang.IllegalArgumentException:x (-65) must be >= 0
相關用法
- Java LongMath log10(long x, RoundingMode mode)用法及代碼示例
- Java Guava IntMath sqrt(int x, RoundingMode mode)用法及代碼示例
- Java IntMath log10(int x, RoundingMode mode)用法及代碼示例
- Java LongMath.divide(long, long, RoundingMode)用法及代碼示例
- Java Guava LongMath pow(long b, int k)用法及代碼示例
- Java Guava LongMath binomial(int n, int k)用法及代碼示例
- Java Guava LongMath isPowerOfTwo(long x)用法及代碼示例
- Java Guava LongMath mod(long x, long m)用法及代碼示例
- Java Guava LongMath mean(long x, long y)用法及代碼示例
- Java Guava LongMath gcd(long a, long b)用法及代碼示例
- Java IntMath.divide(int, int, RoundingMode)用法及代碼示例
- Java LongMath.checkedMultiply(int a, int b)用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Java Guava | sqrt(long x, RoundingMode mode) of LongMath Class with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。