Guava – LongMath.log10方法与实例

Guava – LongMath.log10方法与实例

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

语法:

public static int log10(long x, RoundingMode mode)

参数: 该方法接受2个参数。

  • x 是要找到对数的长值。
  • mode 是指定的舍入模式。

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

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

  • IllegalArgumentException:如果x的值是0或负值。
  • ArithmeticException:如果模式是RoundingMode.UNNECESSARY并且x不是10的幂。

Enum RoundingMode

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

下面给出了一些例子,以便更好地理解实现:

示例1:

// Java code to show implementation of
// log10(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 a1 = 10000;
  
        // Using log10(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.log10(
                a1,
                RoundingMode.HALF_EVEN));
  
        long a2 = 15;
  
        // Using log10(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.log10(a2,
                           RoundingMode.HALF_DOWN));
    }
}

输出:

4
1

例2:

// Java code to show implementation of
// log10(long x, RoundingMode mode) method
// of Guava's LongMath class
  
import java.math.RoundingMode;
import com.google.common.math.LongMath;
  
class GFG {
  
    static int findlog10(long x, RoundingMode mode)
    {
        try {
            // Using log10(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
            int ans = LongMath.log10(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
            findlog10(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#log10-long-java.math.RoundingMode-

赞(1)

评论 抢沙发

评论前必须登录!

 

Guava 教程
Guava 教程Guava - 概述Guava - 环境搭建Guava - Optional类Guava - Preconditions类Guava - Ordering类Guava - Objects类Guava - Range类Guava - Throwables类Guava - Collections 工具类Guava - Caching 工具类Guava - Multiset接口Guava - bimapGuava - MultiMap
Guava Ints类
Guava Ints类Guava Ints - concat() 函数Guava Ints - lastIndexOf() 函数Guava Ints - contains() 函数Guava Ints - asList() 函数Guava Ints - max() 函数Guava Ints - min() 函数Guava Ints - join() 函数Guava Ints - toArray() 函数Guava Ints - indexOf() 函数
Guava Chars类
Guava Chars类Guava - Chars.compare()方法与实例Guava - Chars.hashCode()方法与实例Guava - Chars.min()方法与实例Guava - Chars.max()方法与实例Guava - Chars.toArray()方法与实例Guava - Chars.concat()方法与实例Guava - Chars.contains()方法与实例Guava - Chars.lastIndexOf()方法与实例Guava - Chars.join()方法与实例Guava - Chars.indexOf()方法与实例Guava - Chars.asList()方法与实例
Guava LongMath类
Guava LongMath类Guava - LongMath.binomial方法与实例Guava - LongMath.log2方法与实例Guava - LongMath.pow方法与实例Guava - LongMath.isPowerOfTwo方法与实例Guava - LongMath.gcd方法与实例Guava - LongMath.mean方法与实例Guava - LongMath.sqrt方法与实例Guava - LongMath.mod方法与实例Guava - LongMath.log10方法与实例Guava - LongMath.checkedMultiply方法与实例Guava - LongMath.checkedAdd方法与实例Guava - LongMath.checkedPow方法与实例Guava - LongMath.divide方法与实例
Guava Longs类
Guava Longs类Guava - Longs.compare()方法与实例Guava - Longs.hashCode()方法与实例Guava - Longs.factorial方法与实例Guava - Longs.contains()方法与实例Guava - Longs.min()方法与实例Guava - Longs.max()方法与实例Guava - Longs.toArray()方法与实例Guava - Longs.lastIndexOf()方法与实例Guava - Longs.join()方法与实例Guava - Longs.asList()方法与实例Guava - Longs.checkedSubtract()方法与实例Guava - Longs.concat()方法与实例Guava - Longs.indexOf()方法与实例
Guava Booleans类
Guava Booleans类Guava - Booleans.asList()方法及示例Guava - Booleans.compare()方法及示例Guava - Booleans.concat()方法及示例Guava - Booleans.contains()方法及示例Guava - Booleans.countTrue()方法及示例Guava - Booleans.hashCode()方法及示例Guava - Booleans.indexOf()方法及示例Guava - Booleans.join()方法及示例Guava - Booleans.lastIndexOf()方法及示例Guava - Booleans.toArray()方法及示例
Guava Shorts类
Guava Shorts类Guava - Shorts.toArray()方法及实例Guava - Shorts.min()方法及实例Guava - Shorts.max()方法及实例Guava - Shorts.lastIndexOf()方法及实例Guava - Shorts.join()方法及实例Guava - Shorts.indexOf()方法及实例Guava - Shorts.hashCode()方法及实例Guava - Shorts.contains()方法及实例Guava - Shorts.concat()方法及实例Guava - Shorts.asList()方法及实例
Guava IntMath类
Guava IntMath类Guava - IntMath.checkedAdd()方法及实例Guava - IntMath.checkedMultiply()方法及实例Guava - IntMath.checkedPow()方法及实例Guava - IntMath.checkedSubtract()方法及实例Guava - IntMath.divide()方法及实例Guava - IntMath.log10()方法及实例