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


Java Guava LongMath sqrt(long x, RoundingMode mode)用法及代码示例


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

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



相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Java Guava | sqrt(long x, RoundingMode mode) of LongMath Class with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。