Guava – LongMath.sqrt方法与实例

来源:这里教程网 时间:2026-02-17 21:41:38 作者:

Guava – LongMath.sqrt方法与实例

Guava的LongMath类的sqrt(long x, RoundingMode mode)方法接受两个参数,根据第二个参数指定的舍入模式计算第一个参数的平方根。

语法:

public static long sqrt(long x, RoundingMode mode)

参数: 这个方法接受两个参数。

x:是一个长值,要找到它的平方根。mode:是要进行舍入的舍入模式。

返回值: 该方法返回x的平方根,根据指定的四舍五入模式进行舍入。

异常情况: 此方法抛出以下参数。

IllegalArgumentException: 如果x < 0.ArithmeticException: 如果模式是RoundingMode.UNNECESSARY并且sqrt(x)不是一个整数。

Enum RoundingMode

Enum 常数描述
CEILING向正无穷大舍入的舍入模式。
DOWN向零舍入的舍入模式。
FLOOR四舍五入到负无穷大的模式。
HALF_DOWN向 “最近的邻居 “舍入的舍入模式,除非两个邻居的距离相等,在这种情况下向下舍入。
HALF_EVEN向 “最近的邻居 “四舍五入的模式,除非两个邻居都是等距离,在这种情况下,向偶数邻居四舍五入。
HALF_UP向 “最近的邻居 “四舍五入的模式,除非两个邻居都是等距离的,在这种情况下,向上舍入。
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 : 23Square 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

参考: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/LongMath.html#sqrt-long-java.math.RoundingMode-

相关推荐