【转】Google Guava 数学运算

       为什么使用Guava Math

  • Guava Math针对各种不常见的溢出情况都有充分的测试;对溢出语义,Guava文档也有相应的说明;如果运算的溢出检查不能通过,将导致快速失败。

  • Guava Math的性能经过了精心的设计和调优;虽然性能不可避免地依据具体硬件细节而有所差异,但Guava Math的速度通常可以与Apache Commons的MathUtils相比,在某些场景下甚至还有显著提升。

  • Guava Math在设计上考虑了可读性和正确的编程习惯;IntMath.log2(x, CEILING) 所表达的含义,即使在快速阅读时也是清晰明确的。而32-Integer.numberOfLeadingZeros(x – 1)对于阅读者来说则不够清晰。

一 整数运算

       Guava Math主要处理三种整数类型:int、long和BigInteger。这三种类型的运算工具类分别叫做IntMath、LongMath和BigIntegerMath。

1.1 有溢出检查的运算

       有溢出检查的运算,如果计算结果有溢出的情况下(上溢,下溢),就会抛出ArithmeticException异常。

运算(有溢出检查)IntMath里面方法LongMath里面方法
加法checkedAdd(int a, int b)checkedAdd(int a, int b)
减法checkedSubtract(int a, int b)checkedSubtract(int a, int b)
乘法checkedMultiply(int a, int b)checkedMultiply(int a, int b)
checkedPow(int b, int k)checkedPow(int b, int k)

1.2 上溢,下溢返回最大值最小值

       如果对应的运算如果发生溢出,上溢则返回对应类型的最大值(Integer.MAX_VALUE、Long.MAX_VALUE )、下溢则返回对应类型的最小值(Integer.MIN_VALUE、Long.MIN_VALUE)。

运算IntMath里面方法LongMath里面方法
加法saturatedAdd(int a, int b)saturatedAdd(int a, int b)
减法saturatedSubtract(int a, int b)saturatedSubtract(int a, int b)
乘法saturatedMultiply(int a, int b)saturatedMultiply(int a, int b)
saturatedPow(int b, int k)saturatedPow(int b, int k)

二 实数运算

       IntMath、LongMath和BigIntegerMath提供了很多实数运算的方法,并把最终运算结果舍入成整数。这些方法需要指定一个java.math.RoundingMode枚举值来作为舍入的模式。RoundingMode的取值如下:

RoundingMode枚举值解释
RoundingMode.DOW向零方向舍入(去尾法)
RoundingMode.UP远离零方向舍入
RoundingMode.FLOO向负无限大方向舍入
RoundingMode.CEILING向正无限大方向舍入
RoundingMode.UNNECESSARY不需要舍入,如果用此模式进行舍入,应直接抛出ArithmeticException
RoundingMode.HALF_UP向最近的整数舍入,其中x.5远离零方向舍入
RoundingMode.HALF_DOWN向最近的整数舍入,其中x.5向零方向舍入
RoundingMode.HALF_EVEN向最近的整数舍入,其中x.5向相邻的偶数舍入

实数运算方法

运算IntMath里面方法LongMath里面方法BigIntegerMath里面方法
除法divide(int, int, RoundingMode)divide(long, long, RoundingMode)divide(BigInteger, BigInteger, RoundingMode)
2为底的对数log2(int, RoundingMode)log2(long, RoundingMode)log2(BigInteger, RoundingMode)
10为底的对数log10(int, RoundingMode)log10(long, RoundingMode)log10(BigInteger, RoundingMode)
平方根sqrt(int, RoundingMode)sqrt(long, RoundingMode)sqrt(BigInteger, RoundingMode)

实数运算部分Guava还另外提供了一些有用的运算函数

运算IntMath里面方法LongMath里面方法BigIntegerMath里面方法
最大公约数gcd(int, int)gcd(long, long)gcd(BigInteger)
取模mod(int, int)mod(long, long)mod(BigInteger)
取幂pow(int, int)pow(long, int)pow(int)
是否2的幂isPowerOfTwo(int)isPowerOfTwo(long)isPowerOfTwo(BigInteger)
阶乘*factorial(int)factorial(int)factorial(int)
二项式系数*binomial(int, int)binomial(int, int)binomial(int, int)

*阶乘和二项式系数的运算结果如果溢出,则返回MAX_VALUE

三 浮点数运算

       JDK已经比较彻底地涵盖了浮点数运算,但Guava在DoubleMath类中也提供了一些有用的方法。

运算DoubleMath方法
判断该浮点数是不是一个整数isMathematicalInteger(double)
舍入为int;对无限小数、溢出抛出异常roundToInt(double, RoundingMode)
舍入为long;对无限小数、溢出抛出异常roundToLong(double, RoundingMode)
舍入为BigInteger;对无限小数抛出异常roundToBigInteger(double, RoundingMode)
2的浮点对数,并且舍入为int,比JDK的Math.log(double) 更快log2(double, RoundingMode)

       关于Guava属性运算的一些帮助类就这些。使用非常简单。我们就不举例了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值