LongMath提供long基础类型的实用方法。

## 类声明

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

## 方法

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

1 static long checkedAdd(long a, long b)

2 static long checkedMultiply(long a, long b)

3 static long checkedPow(long b, int k)

4 static long checkedSubtract(long a, long b)

5 static long divide(long p, long q, RoundingMode mode)

6 static long factorial(int n)

7 static long gcd(long a, long b)

8 static boolean isPowerOfTwo(long x)

9 static int log10(long x, RoundingMode mode)

10 static int log2(long x, RoundingMode mode)

11 static long mean(long x, long y)

12 static int mod(long x, int m)

13 static long mod(long x, long m)

14 static long pow(long b, int k)

15 static long sqrt(long x, RoundingMode mode)

## 方法继承

• java.lang.Object

## LongMath 示例

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

public class GuavaTester {

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

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

System.out.println(LongMath.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(LongMath.divide(100, 3, RoundingMode.UNNECESSARY));
}catch(ArithmeticException e){
System.out.println("Error: " + e.getMessage());
}

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

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

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

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

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

System.out.println("factorial(5): "+LongMath.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```