Java BigInteger学习心得

如果要处理大数据可以使用BigInteger。

1.valueOf(parament); 将参数转换为制定的类型

   比如 int a=1;

       BigInteger b=BigInteger.valueOf(a);

     则b=1; 

         String s=”12”;

       BigInteger c=BigInteger.valueOf(s);

       则c=12;

 返回其值等于指定 long 的值的 BigInteger。提供的此“静态工厂方法”优先于 (long) 构造方法,因为前者允许重用经常使用的 BigInteger。 


2.add(); 大整数相加

   BigInteger a=new BigInteger(“12”);

   BigInteger b=new BigInteger(“34”);

   

 System.out.println(a.add(b));    46

返回其值为 (this + val) 的 BigInteger。 


3.subtract(); 相减

返回其值为 (this - val) 的 BigInteger。 

4.multiply(); 相乘

返回其值为 (this * val) 的 BigInteger。 

5.divide();    相除取整

返回其值为 (this / val) 的 BigInteger。 

6.remainder(); 取余

返回其值为 (this % val) 的 BigInteger。

7.pow();   a.pow(b)=a^b

返回其值为 (thisexponent) 的 BigInteger。注意,exponent 是一个整数而不是 BigInteger。 

8.gcd();   最大公约数

返回一个 BigInteger,其值是 abs(this) abs(val) 的最大公约数。如果 this==0 && val==0,则返回 0 。 

9.abs(); 绝对值

返回其值是此 BigInteger 的绝对值的 BigInteger。

10.negate(); 取反数

返回此 BigInteger 的正负号函数。

11.mod(); a.mod(b)=a%b=a.remainder(b);

返回其值为 (this mod m) 的 BigInteger。此方法不同于 remainder,因为它始终返回一个 非负 BigInteger。

12.max(); 返回此 BigInteger 和 val 的最大值。 

min();返回此 BigInteger 和 val 的最小值。 

13.comareTo();

将此 BigInteger 与指定的 BigInteger 进行比较。对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=) 中的每一个运算符的各个方法,优先提供此方法。执行这些比较的建议语句是:(x.compareTo(y) <op> 0),其中 <op> 是六个比较运算符之一。 

14.equals(); 是否相等

比较此 BigInteger 与指定的 Object 的相等性(boolean)



	BigInteger num1 = new BigInteger("9999999999999999999999999999999999999999999999999999999");  
	BigInteger num2 = new BigInteger("9999999999999999999999999999999999999999999999999999999");  
	System.out.println("num1+num2=" + num1.add(num2));  
	System.out.println("num1-num2=" + num1.subtract(num2));  
	System.out.println("num1*num2=" + num1.multiply(num2));  
	System.out.println("num1/num2=" + num1.divide(num2));  
	System.out.println("num1%num2=" + num1.mod(num2));  






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值