Guava – LongMath.log2方法与实例

Guava – LongMath.log2方法与实例

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

语法:

public static int log2(long x, RoundingMode mode)

参数: 该方法接受2个参数,其中x是一个long,mode是指定的舍入模式。

返回值: 该方法返回x的Base-2对数,根据指定的四舍五入模式进行舍入。

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

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

Enum四舍五入模式

Enum 常数 描述
CEILING 向正无穷大舍入的舍入模式。
DOWN 趋近于零的舍入模式。
FLOOR 四舍五入到负无穷大的模式。
HALF_DOWN 向 “最近的邻居 “舍入的舍入模式,除非两个邻居的距离相等,在这种情况下向下舍入。
HALF_EVEN 向 “最近的邻居 “四舍五入的模式,除非两个邻居都是等距离,在这种情况下,向偶数邻居四舍五入。
HALF_UP 向 “最近的邻居 “四舍五入的模式,除非两个邻居都是等距离的,在这种情况下,向上舍入。
UNNECESSARY 四舍五入模式断言请求的操作有一个精确的结果,因此不需要四舍五入。
UP 四舍五入模式是指从零开始舍入。

下面的例子说明了Guava LongMath的log2()方法的实现。

例子1 :

// Java code to show implementation of
// log2(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 n1 = 1111;
  
        // Using log2(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.
        System.out.println(LongMath.log2(
            n1,
            RoundingMode.HALF_EVEN));
  
        long n2 = 205;
  
        // Using log2(long x, RoundingMode mode)
        // method of Guava's LongMath class
        // The RoundingMode HALF_DOWN rounds towards
        // "nearest neighbor" unless both neighbors
        // are equidistant, in which case round down.
        System.out.println(LongMath.log2(
            n2,
            RoundingMode.HALF_DOWN));
    }
}

输出:

10
8

例子2:

// Java code to show implementation of
// log2(long x, RoundingMode mode) method
// of Guava's LongMath class
  
import java.math.RoundingMode;
import com.google.common.math.LongMath;
  
class GFG {
  
    static long findlog2(long x, RoundingMode mode)
    {
        try {
            // Using log2(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 ans = LongMath.log2(x, mode);
  
            // Return the answer
            return ans;
        }
        catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        long x = -122;
  
        try {
  
            // Function calling
            findlog2(x, RoundingMode.HALF_EVEN);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

输出:

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

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程