Guava IntMath类

Guava IntMath类

简介 : IntMath 是用来对整数值进行数学运算的。基本的独立数学函数根据所涉及的主要数字类型被分为IntMath、LongMath、DoubleMath和BigIntegerMath类。这些类具有平行结构,但每个类只支持相关的函数子集。

声明 : com.google.common.math.IntMath类的声明是。

@GwtCompatible(emulated = true)
public final class IntMath
   extends Object

下表显示了Guava的IntMath类所提供的一些方法。

Guava IntMath类

异常情况:

  • log2 : 如果x<=0,则出现IllegalArgumentException
  • log10 : 如果x<=0,则出现IllegalArgumentException
  • pow : 如果k<0,则出现IllegalArgumentException。
  • sqrt : 如果x<0,则出现IllegalArgumentException
  • divide : 如果q == 0,或者mode == UNNECESSARY,并且a不是b的整数倍,则出现ArithmeticException。
  • mod : 如果m<=0,则出现算术异常
  • gcd : 如果a<0或b<0,则出现IllegalArgumentException。
  • checkedAdd : 如果a+b在有符号int算术中溢出,则出现ArithmeticException
  • checkedSubtract : 如果a-b在有符号整数运算中溢出,则会出现ArithmeticException
  • checkedMultiply : 如果a * b在有符号int算术中溢出,则出现ArithmeticException
  • checkedPow : 如果b在有符号的int算术中溢出到第k次幂,则出现ArithmeticException
  • factorial : 如果n<0,则出现IllegalArgumentException
  • binomial : 如果n<0,k n,则出现IllegalArgumentException。

Guava的IntMath类提供的其他一些方法有:

Guava IntMath类

例1 :

// Java code to show implementation of
// IntMath Class of Guava
import java.math.RoundingMode;
import com.google.common.math.IntMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
  
        // Creating an object of GFG class
        GFG obj = new GFG();
  
        // Function calling
        obj.examples();
    }
  
    private void examples()
    {
  
        try {
  
            // exception will be thrown as 80 is not
            // completely divisible by 3
            // thus rounding is required, and
            // RoundingMode is set as UNNESSARY
            System.out.println(IntMath.divide(80, 3,
                           RoundingMode.UNNECESSARY));
        }
        catch (ArithmeticException ex) {
            System.out.println("Error Message is : " + 
                                     ex.getMessage());
        }
    }
}

输出:

Error Message is : mode was UNNECESSARY, but rounding was necessary

例2 :

// Java code to show implementation of
// IntMath Class of Guava
import java.math.RoundingMode;
import com.google.common.math.IntMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
        // Creating an object of GFG class
        GFG obj = new GFG();
  
        // Function calling
        obj.examples();
    }
  
    private void examples()
    {
  
        // As 120 is divisible by 4, so
        // no exception is thrown
        System.out.println(IntMath.divide(120, 4,
                        RoundingMode.UNNECESSARY));
  
        // To compute GCD of two integers
        System.out.println("GCD is : " + 
                             IntMath.gcd(70, 14));
  
        // To compute log to base 10
        System.out.println("Log10 is : " +
          IntMath.log10(1000, RoundingMode.HALF_EVEN));
  
        // To compute remainder
        System.out.println("modulus is : " +
                          IntMath.mod(125, 5));
  
        // To compute factorial
        System.out.println("factorial is : " +
                           IntMath.factorial(7));
  
        // To compute log to base 2
        System.out.println("Log2 is : " + 
               IntMath.log2(8, RoundingMode.HALF_EVEN));
  
        // To compute square root
        System.out.println("sqrt is : " +
                    IntMath.sqrt(IntMath.pow(12, 2),
                          RoundingMode.HALF_EVEN));
    }
}

输出:

30
GCD is : 14
Log10 is : 3
modulus is : 0
factorial is : 5040
Log2 is : 3
sqrt is : 12

参考资料 : Google Guava

赞(0)

猜你喜欢

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址
    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()方法及实例