Guava LongMath类

来源:这里教程网 时间:2026-02-17 21:39:13 作者:

Guava LongMath类

LongMath在long类型上提供了实用方法。

类声明

以下是 com.google.common.math.LongMath 类的声明。

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

方法

Sr.NoMethod & Description
1static long binomial(int n, int k) 计算n个元素中取k个元素的组合数,或者如果结果无法用long表示,则返回Long.MAX_VALUE。
2static long checkedAdd(long a, long b) 返回a和b的和,如果结果不会溢出的话。
3static long checkedMultiply(long a, long b) 返回a和b的乘积,如果结果不会溢出的话。
4static long checkedPow(long b, int k) 返回b的k次幂,如果结果不会溢出的话。
5static long checkedSubtract(long a, long b) 返回a和b的差,前提是它不会溢出。
6static long divide(long p, long q, RoundingMode mode) 返回通过使用指定的RoundingMode对p除以q的结果进行四舍五入。
7static long factorial(int n) 返回n!,即前n个正整数的乘积,如果n == 0,则返回1,如果结果不适合long,则返回Long.MAX_VALUE。
8static long gcd(long a, long b) 返回a和b的最大公约数。
9static boolean isPowerOfTwo(long x) 如果x表示2的幂,则返回true。
10static int log10(long x, RoundingMode mode) 返回x的以10为底的对数,根据指定的舍入模式进行四舍五入。
11static int log2(long x, RoundingMode mode) 返回x的以2为底的对数,根据指定的舍入模式进行四舍五入。
12static long mean(long x, long y) 返回x和y的算术平均值,向负无穷舍入。
13static int mod(long x, int m) 返回x对m取模,一个小于m的非负值。
14static long mod(long x, long m) 返回x对m取模,一个小于m的非负值。
15static long pow(long b, int k) 返回b的k次幂。
16static long sqrt(long x, RoundingMode mode) 返回x的平方根,使用指定的舍入模式进行四舍五入。

继承的方法

这个类继承了以下类的方法:

java.lang.Object

LongMath类的示例

使用任何编辑器在C:/> Guava位置创建以下java程序:

GuavaTester.java

import java.math.RoundingMode;import com.google.common.math.LongMath;public class GuavaTester {   public static void main(String args[]) {      GuavaTester tester = new GuavaTester();      tester.testLongMath();   }   private void testLongMath() {      try {         System.out.println(LongMath.checkedAdd(Long.MAX_VALUE, Long.MAX_VALUE));      } 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));   }}

验证结果

使用 javac 编译器编译类,如下所示−

C:\Guava>javac GuavaTester.java

现在运行GuavaTester以查看结果。

C:\Guava>java GuavaTester

查看结果。

Error: overflow20Error: mode was UNNECESSARY, but rounding was necessaryLog2(2): 1Log10(10): 1sqrt(100): 10gcd(100,50): 50modulus(100,50): 0factorial(5): 120

相关推荐