java大整数类(超清晰)

java刷题目遇到大整数真的不要太舒服,但我发现java的大整数类功能贼多,自己掌握的很少,所以写篇博客学习一下…

BigInteger和BigDecimal都位于java.math包中(不要和java.lang.Math搞混了!!).
api文档中是这么介绍java.math包的:

Provides classes for performing arbitrary-precision integer arithmetic (BigInteger) and arbitrary-precision decimal arithmetic (BigDecimal). BigInteger is analogous to the primitive integer types except that it provides arbitrary precision, hence operations on BigIntegers do not overflow or lose precision. In addition to standard arithmetic operations, BigInteger provides modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations. BigDecimal provides arbitrary-precision signed decimal numbers suitable for currency calculations and the like. BigDecimal gives the user complete control over rounding behavior, allowing the user to choose from a comprehensive set of eight rounding modes.
提供用于执行M任意精度整数算术(BigInteger)和任意精度十进制算术(BigDecimal)的类。 BigInteger与原始整数类型类似,不同之处在于它提供任意精度,因此对BigIntegers的操作不会溢出或丢失精度。除标准算术运算外, BigInteger还提供模块化算术,GCD计算,素数测试,素数生成,位操作和其他一些其他运算。 BigDecimal提供适用于货币计算等的任意精度的带符号十进制数。 BigDecimal为用户提供了对舍入行为的完全控制,使用户可以从八种舍入模式的全面选择中进行选择(我是不是贼贴心嘿嘿嘿)

BigInteger类提供了四个最基本的BigInteger常量,分别是

		System.out.println(BigInteger.ONE);//输出1
    	System.out.println(BigInteger.TEN);//输出10
    	System.out.println(BigInteger.TWO);//输出2
    	System.out.println(BigInteger.ZERO);//输出0
1
10
2
0

就BigInteger而言,他有八个构造函数:

  • BigInteger​(byte[] val)
    -将包含BigInteger的二进制补码二进制表示形式的字节数组转换为BigInteger。
  • BigInteger​(byte[] val, int off, int len)
    -将包含BigInteger的二进制补码二进制表示形式的字节子数组转换为BigInteger。
  • BigInteger​(int signum, byte[] magnitude)
    -将BigInteger的符号幅度表示转换为BigInteger。
  • BigInteger​(int signum, byte[] magnitude, int off, int len)
    将BigInteger的符号幅度表示转换为BigInteger
  • BigInteger​(int bitLength, int certainty, Random rnd)
    使用指定的bitLength构造一个随机生成的可能为素数的正BigInteger。
  • BigInteger​(int numBits, Random rnd)
    构造一个随机生成的BigInteger,均匀分布在0到(2^numBits -1)之间(包括0和2 )
  • BigInteger​(String val)
    将BigInteger的十进制String表示转换为BigInteger。
  • BigInteger​(String val, int radix) 将指定基数中的BigInteger的String表示转换为BigInteger。

就写题目而言常用的好像就下面俩构造函数=-=
以下是大整数相关的api

import java.util.*;
import java.math.BigInteger;
import java.math.*;
public class Main{
   
    
    public static void main(String[] args) {
   
    	//随便打的两个数,不过用生成随机大整数不是更香嘛
    	BigInteger number1=new BigInteger("347238462384523623645237465237415234165234615246742354");
    	BigInteger number2=new BigInteger("42673547263541874637462394142837645");
    	//返回一个BigInteger,其值为该BigInteger的绝对值。
    	System.out.println("abs():"+number1.abs().toString());
    	//返回值为BigInteger (this / val)。
    	System.out.println("divide():"+number1.divide(number2));
    	//返回值为的BigInteger (this % val)。
    	System.out.println("remainder():"+number1.remainder(number2));
    	//返回值为的BigInteger (this + val)。
    	System.out.println("add():"+number1
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值