IntMath提供整型的实用方法。

## 类声明

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

## 方法

S.N. 方法及说明
1 static int binomial(int n, int k)

2 static int checkedAdd(int a, int b)

3 static int checkedMultiply(int a, int b)

4 static int checkedPow(int b, int k)

5 static int checkedSubtract(int a, int b)

6 static int divide(int p, int q, RoundingMode mode)

7 static int factorial(int n)

8 static int gcd(int a, int b)

9 static boolean isPowerOfTwo(int x)

10 static int log10(int x, RoundingMode mode)

11 static int log2(int x, RoundingMode mode)

12 static int mean(int x, int y)

13 static int mod(int x, int m)

14 static int pow(int b, int k)

15 static int sqrt(int x, RoundingMode mode)

## 方法继承

• java.lang.Object

## IntMath 例子

GuavaTester.java
```import java.math.RoundingMode;

public class GuavaTester {

public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testIntMath();
}

private void testIntMath(){
try{
}catch(ArithmeticException e){
System.out.println("Error: " + e.getMessage());
}

System.out.println(IntMath.divide(100, 5, RoundingMode.UNNECESSARY));
try{
//exception will be thrown as 100 is not completely divisible by 3 thus rounding
// is required, and RoundingMode is set as UNNESSARY
System.out.println(IntMath.divide(100, 3, RoundingMode.UNNECESSARY));
}catch(ArithmeticException e){
System.out.println("Error: " + e.getMessage());
}

System.out.println("Log2(2): "+IntMath.log2(2, RoundingMode.HALF_EVEN));

System.out.println("Log10(10): "+IntMath.log10(10, RoundingMode.HALF_EVEN));

System.out.println("sqrt(100): "+IntMath.sqrt(IntMath.pow(10,2), RoundingMode.HALF_EVEN));

System.out.println("gcd(100,50): "+IntMath.gcd(100,50));

System.out.println("modulus(100,50): "+IntMath.mod(100,50));

System.out.println("factorial(5): "+IntMath.factorial(5));
}
}```

## 验证结果

```C:\Guava>javac GuavaTester.java
```

```C:\Guava>java GuavaTester
```

```Error: overflow
20
Error: mode was UNNECESSARY, but rounding was necessary
Log2(2): 1
Log10(10): 1
sqrt(100): 10
gcd(100,50): 50
modulus(100,50): 0
factorial(5): 120```