BigInteger类的使用方法

11 篇文章 0 订阅

先看一下JAVA API中对BigInteger的解释: 

public class BigInteger
extends Number
implements Comparable<BigInteger>

Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types). BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations

 翻译一下大概是这样的:不可变的任意精度整数。所有操作都表现为用两个带符号(如Java的原始整数类型)表示子表达式。BigCype为Java的原始整数运算符提供了类似的工具,并提供了JavaLang.Maple的所有相关方法。此外,BigInteger还提供了模块化算术、GCD计算、素数测试、素数生成、位操作和其他一些杂项操作的操作。

BigInteger的主要构造方法:

//将包含BigInteger的二进制补码二进制表达式的字节数组转换为BigInteger。  
BigInteger(byte[] val) ;

//将BigInteger的符号大小表示形式转换为BigInteger。  
BigInteger(int signum, byte[] magnitude) ;

//构造一个随机生成的正BigInteger,它可能是素数,具有指定的bitLength。  
BigInteger(int bitLength, int certainty, Random rnd) ;

//构造一个随机生成的BigInteger,均匀分布在0到(2 numBits - 1)的范围内。  
BigInteger(int numBits, Random rnd) ;

//将BigInteger的十进制字符串表示形式转换为BigInteger。  
BigInteger(String val) ;

//将指定基数中的BigInteger的String表示形式转换为BigInteger。  
BigInteger(String val, int radix) ;

BigInteger的主要运算方法:

加法运算:

/**
*@parm val
*要添加到这个BigInteger的值
*
*@return 
*this+val
*/
public BigInteger add(BigInteger val)

减法运算:

/**
*@parm val
*要从这个BigInteger减去的值
*
*@return 
*this-val
*/
public BigInteger subtract(BigInteger val)

乘法运算:

/**
*@parm val
*要乘以这个BigInteger的值
*
*@return 
*this*val
*/
public BigInteger multiply(BigInteger val)

除法运算:

/**
*@parm val
*这个BigInteger要被除以值
*
*@return 
*this/val
*/
public BigInteger divide(BigInterger val)

绝对值运算: abs()

取余运算(%):remainder(BigInteger val)

指数计算(^):pow(int exponent)

…………

代码测试

代码:

package BigIntegerTest;

import java.math.BigInteger;

public class BigIntegerCalc {
	public static void main(String[] args){
		BigInteger a = new BigInteger("0");			//0
		BigInteger b = new BigInteger("1");			//1
		BigInteger c = new BigInteger("10",2);		//2
		
		System.out.println(b.add(c));
		try {
			System.out.println(b.divide(a));			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

输出:

3
BigInteger divide by zero

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值