Guava IntMath类

 

Guava IntMath类

IntMath提供整型的实用方法。

类声明

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

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

方法

 

方法继承

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

java.lang.Object

IntMath 例子http://www.wityx.com/post/1270_1_1.html

选择使用任何编辑器创建以下java程序在 C:/> Guava GuavaTester.java

import java.math.RoundingMode;
import com.google.common.math.IntMath;

public class GuavaTester {

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

   private void testIntMath(){
      try{
         System.out.println(IntMath.checkedAdd(Integer.MAX_VALUE, Integer.MAX_VALUE));
      }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));
   }
}

验证结果

使用javac编译器编译如下类

C:Guava>javac GuavaTester.java

现在运行GuavaTester看到的结果

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值