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


Java Guava BigIntegerMath log2()用法及代码示例


Guava BigIntegerMath类的方法log2(BigInteger x,RoundingMode模式)返回x的以2为底的对数,并根据指定的舍入模式进行四舍五入。

用法:

public static int log2(BigInteger x, RoundingMode mode)

参数:此方法采用以下参数:


  • x:要查找其日志的BigInteger号。
  • mode:用于计算2的对数的舍入模式。

返回值:此方法返回x的以2为底的对数,并根据指定的舍入模式进行舍入。

异常:此方法引发以下异常:

  • IllegalArgumentException:如果x <= 0。
  • ArithmeticException:如果mode为RoundingMode.UNNECESSARY且x不是2的幂。

Enum RoundingMode

枚举常量 描述
CEILING 舍入模式向正无穷大舍入。
DOWN 舍入模式向零舍入。
FLOOR 舍入模式向负无穷大舍入。
HALF_DOWN 除非两个邻居等距,否则舍入模式将朝“nearest neighbor”舍入。
HALF_EVEN 舍入模式向“nearest neighbor”舍入,除非两个邻居都等距,在这种情况下,向偶数邻居舍入。
HALF_UP 除非两个邻居等距,否则舍入模式将朝“nearest neighbor”舍入。
UNNECESSARY 舍入模式可以断言所请求的操作具有准确的结果,因此不需要舍入。
UP 舍入模式从零舍入。

以下示例说明了BigIntegerMath.log2()方法:

范例1:

// Java code to show implementation of 
// log2(BigInteger x, RoundingMode mode) method 
// of Guava's BigIntegerMath class 
  
import java.math.*; 
import com.google.common.math.BigIntegerMath; 
  
class GFG { 
  
    // Driver code 
    public static void main(String args[]) 
    { 
        BigInteger a1 = BigInteger.valueOf(1024); 
  
        // Using log2(BigInteger x, RoundingMode mode) 
        // method of Guava's BigIntegerMath class 
        // The RoundingMode HALF_EVEN rounds towards 
        // the "nearest neighbor" unless both neighbors 
        // are equidistant, in which case, round towards 
        // the even neighbor. 
        System.out.println("Log base 2 of " + a1 
                           + " with rounding mode HALF_EVEN = "
                           + BigIntegerMath 
                                 .log2(a1, 
                                        RoundingMode.HALF_EVEN)); 
  
        BigInteger a2 = BigInteger.valueOf(15); 
  
        // Using log2(BigInteger x, RoundingMode mode) 
        // method of Guava's BigIntegerMath class 
        // The RoundingMode HALF_DOWN rounds towards 
        // "nearest neighbor" unless both neighbors 
        // are equidistant, in which case round down. 
        System.out.println("Log base 2 of " + a2 
                           + " with rounding mode HALF_DOWN = "
                           + BigIntegerMath 
                                 .log10(a2, 
                                        RoundingMode.HALF_DOWN)); 
    } 
}

输出:

Log base 2 of 1024 with rounding mode HALF_EVEN = 10
Log base 2 of 15 with rounding mode HALF_DOWN = 1

范例2:显示IllegalArgumentException

// Java code to show implementation of 
// log2(BigInteger x, RoundingMode mode) method 
// of Guava's BigIntegerMath class 
  
import java.math.*; 
import com.google.common.math.BigIntegerMath; 
  
class GFG { 
  
    // Driver code 
    public static void main(String args[]) 
    { 
  
        try { 
            BigInteger a1 = BigInteger.valueOf(-5); 
  
            // Using log2(BigInteger x, RoundingMode mode) 
            // method of Guava's BigIntegerMath 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 a1 <= 0 
            System.out.println("Log base 2 of " + a1 
                               + " with rounding mode HALF_EVEN = "
                               + BigIntegerMath 
                                     .log2(a1, 
                                            RoundingMode.HALF_EVEN)); 
        } 
  
        catch (Exception e) { 
            System.out.println("Exception:" + e); 
        } 
    } 
}

输出:

Exception:java.lang.IllegalArgumentException:x (-5) must be > 0

参考: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/math/BigIntegerMath.html#log2-java.math.BigInteger-java.math.RoundingMode-



相关用法


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