什么是大数?大数有什么用?如何使用大数?

0 什么是大数

如果基本的整数和浮点数精度不能够满足需求,那么可以使用 javamath 包中两个很有用的类:BigInteger和 BiDecimal。
这两个类可以处理包含任意长度数字序列的数值。
BigInteger类实现任意精度的整数运算,BigDecimal实现任意精度的浮点数运算。

1 大数有什么用

在特定的时候必须用大数,不然会发生错误;
例如:

        for (int  i = 1; i < 11 ; i++) {
            System.out.println(i*0.1);
        }

正常来说会输出0.1~1
但是结果为:
在这里插入图片描述

所以说,这时候就需要用大数来处理了。

假设用大数:

        BigDecimal bigDecimal = BigDecimal.valueOf(0.1);
        for (int i = 1; i < 11 ; i++) {
            System.out.println(bigDecimal.multiply(BigDecimal.valueOf(i)));
        }

结果就正常了。
在这里插入图片描述

2 使用大数

构造大数:
使用静态的 valueof 方法可以将普通的数值转换为大数:BigInteger a= BigInteger.value0f(100);
大数计算:
不能使用算术运算符(如:+和*)处理大数,而需要使用大数类中的add和multiply方法。
还有一些常量:
在这里插入图片描述
在这里插入图片描述

大数的所有方法

BigInteger

Modifier and TypeMethod and Description
BigIntegerabs() 返回的值BigInteger是BigInteger的绝对值。
BigIntegeradd(BigInteger val) 返回的值是 (this + val) BigInteger。
BigIntegerand(BigInteger val) 返回的值是 (this & val) BigInteger。
BigIntegerandNot(BigInteger val) 返回的值是 (this & ~val) BigInteger。
intbitCount() 返回在补码表示这个BigInteger不同于其符号位的比特数。
intbitLength() 返回最小的二进制补码表示这个BigInteger的比特数,不包括符号位。
bytebyteValueExact() 将这一 byte BigInteger,检查丢失的信息。
BigIntegerclearBit(int n) 返回一个BigInteger其值等效于这个BigInteger与指定点清除。
intcompareTo(BigInteger val) 这个BigInteger与指定BigInteger比较。
BigIntegerdivide(BigInteger val) 返回的值是 (this / val) BigInteger。
BigInteger[]divideAndRemainder(BigInteger val) 返回两个关于大整数包含 (this / val)随后 (this % val)数组。
doubledoubleValue() 将这一 double BigInteger。
booleanequals(Object x) 这与平等BigInteger指定对象比较。
BigIntegerflipBit(int n) 返回一个BigInteger其值等效于这个BigInteger与指定的位翻转。
floatfloatValue() 将这一 float BigInteger。
BigIntegergcd(BigInteger val) 返回的值是 abs(this) BigInteger和 abs(val)最大公约数。
intgetLowestSetBit() 返回最右边的指数(低级)在BigInteger这一点(在最右边的一位右零比特数)。
inthashCode() 返回此BigInteger的哈希代码。
intintValue() BigInteger将这一 int
intintValueExact() 将这一 int BigInteger,检查丢失的信息。
booleanisProbablePrime(int certainty) 返回 true如果这很可能就是BigInteger总理, false如果它肯定复合。
longlongValue() 将这一 long BigInteger。
longlongValueExact() 将这一 long BigInteger,检查丢失的信息。
BigIntegermax(BigInteger val) 返回此BigInteger和 val最大。
BigIntegermin(BigInteger val) 返回此BigInteger和 val最小。
BigIntegermod(BigInteger m) 返回一个BigInteger的价值 (this mod m)。
BigIntegermodInverse(BigInteger m) 返回的值是 (this - 1 mod m) BigInteger。
BigIntegermodPow(BigInteger exponent, BigInteger m) 返回的值是 (thisexponent mod m) BigInteger。
BigIntegermultiply(BigInteger val) 返回的值是 (this * val) BigInteger。
BigIntegernegate() 返回的值是 (-this) BigInteger。
BigIntegernextProbablePrime() 返回第一个整数大于这个 BigInteger可能是素数。
BigIntegernot() 返回的值是 (~this) BigInteger。
BigIntegeror(BigInteger val) 返回的值是 `(this
BigIntegerpow(int exponent) 返回的值是 (thisexponent) BigInteger。
static BigIntegerprobablePrime(int bitLength, Random rnd) 返回一个正的BigInteger可能是素数,用指定的个位长度。
BigIntegerremainder(BigInteger val) 返回的值是 (this % val) BigInteger。
BigIntegersetBit(int n) 返回一个BigInteger其值等效于这个BigInteger与指定的点集。
BigIntegershiftLeft(int n) 返回的值是 (this << n) BigInteger。
BigIntegershiftRight(int n) 返回的值是 (this >> n) BigInteger。
shortshortValueExact() 将这一 short BigInteger,检查丢失的信息。
intsignum() 返回此BigInteger的符号函数。
BigIntegersubtract(BigInteger val) 返回的值是 (this - val) BigInteger。
booleantestBit(int n) 返回 true当且仅当指定的位设置。
byte[]toByteArray() 返回一个包含此BigInteger的二进制补码表示的字节数组。
StringtoString() 返回此BigInteger十进制字符串表示形式。
StringtoString(int radix) 返回在给定的基本BigInteger的字符串表示形式。
static BigIntegervalueOf(long val) 返回一个BigInteger其值等于指定的 long
BigIntegerxor(BigInteger val) 返回的值是 (this ^ val) BigInteger。

BigDecimal

Modifier and TypeMethod and Description
BigDecimalabs() 返回一个 BigDecimal其价值是本 BigDecimal的绝对值,其规模 this.scale()
BigDecimalabs(MathContext mc) 返回一个 BigDecimal其价值是本 BigDecimal绝对值舍入根据语境设置。
BigDecimaladd(BigDecimal augend) 返回的值是 BigDecimal (this + augend),其规模 max(this.scale(), augend.scale())
BigDecimaladd(BigDecimal augend, MathContext mc) 返回的值是 BigDecimal (this + augend),舍入根据语境的设置。
bytebyteValueExact() 将这一 byte BigDecimal,检查丢失的信息。
intcompareTo(BigDecimal val)BigDecimal与指定的 BigDecimal比较。
BigDecimaldivide(BigDecimal divisor) 返回的值是 BigDecimal (this / divisor),和其首选的规模 (this.scale() - divisor.scale());如果准确的商不能代表(因为它有一个十进制小数点扩展)的 ArithmeticException抛出。
BigDecimaldivide(BigDecimal divisor, int roundingMode) 返回的值是 BigDecimal (this / divisor),其规模 this.scale()
BigDecimaldivide(BigDecimal divisor, int scale, int roundingMode) 返回的值是 BigDecimal (this / divisor),其规模为指定的。
BigDecimaldivide(BigDecimal divisor, int scale, RoundingMode roundingMode) 返回的值是 BigDecimal (this / divisor),其规模为指定的。
BigDecimaldivide(BigDecimal divisor, MathContext mc) 返回的值是 BigDecimal (this / divisor),舍入根据语境的设置。
BigDecimaldivide(BigDecimal divisor, RoundingMode roundingMode) 返回的值是 BigDecimal (this / divisor),其规模 this.scale()
BigDecimal[]divideAndRemainder(BigDecimal divisor) 返回一二元 BigDecimal数组包含的结果 divideToIntegralValue随后的结果 remainder在两个操作数。
BigDecimal[]divideAndRemainder(BigDecimal divisor, MathContext mc) 返回一二元 BigDecimal数组包含的结果 divideToIntegralValue随后对两个操作数舍入根据上下文设置 remainder计算结果。
BigDecimaldivideToIntegralValue(BigDecimal divisor) 返回的值是 BigDecimal(this / divisor)整数部分向下舍入。
BigDecimaldivideToIntegralValue(BigDecimal divisor, MathContext mc) 返回的值是 BigDecimal (this / divisor)整数部分。
doubledoubleValue() 将这一 double BigDecimal
booleanequals(Object x)BigDecimal与平等的规定 Object比较。
floatfloatValue() 将这一 float BigDecimal
inthashCode() 返回此 BigDecimal哈希代码。
intintValue() 将这一 int BigDecimal
intintValueExact() 将这一 int BigDecimal,检查丢失的信息。
longlongValue() 将这一 long BigDecimal
longlongValueExact() 将这一 long BigDecimal,检查丢失的信息。
BigDecimalmax(BigDecimal val) 返回该 BigDecimalval最大。
BigDecimalmin(BigDecimal val) 返回该 BigDecimalval最小。
BigDecimalmovePointLeft(int n) 返回一个 BigDecimal相当于一个与小数点移动 n地方留下。
BigDecimalmovePointRight(int n) 返回一个 BigDecimal相当于这一 n小数点移动到正确的地方。
BigDecimalmultiply(BigDecimal multiplicand) 返回的值是 BigDecimal (this × multiplicand),其规模 (this.scale() + multiplicand.scale())
BigDecimalmultiply(BigDecimal multiplicand, MathContext mc) 返回的值是 BigDecimal (this × multiplicand),舍入根据语境的设置。
BigDecimalnegate() 返回的值是 BigDecimal (-this),其规模 this.scale()
BigDecimalnegate(MathContext mc) 返回的值是 BigDecimal (-this),舍入根据语境的设置。
BigDecimalplus() 返回的值是 BigDecimal (+this),其规模 this.scale()
BigDecimalplus(MathContext mc) 返回的值是 BigDecimal (+this),舍入根据语境的设置。
BigDecimalpow(int n) 返回的值是 BigDecimal (thisn),权力是精确计算,无限精度。
BigDecimalpow(int n, MathContext mc) 返回的值是 (thisn) BigDecimal
intprecision() 返回该 BigDecimal的精度。
BigDecimalremainder(BigDecimal divisor) 返回的值是 (this % divisor) BigDecimal
BigDecimalremainder(BigDecimal divisor, MathContext mc) 返回的值是 BigDecimal (this % divisor),舍入根据语境的设置。
BigDecimalround(MathContext mc) 返回一个 BigDecimal圆形根据 MathContext设置。
intscale() 返回该 BigDecimal的规模。
BigDecimalscaleByPowerOfTen(int n) 返回来的值等于( this×10 n)。
BigDecimalsetScale(int newScale) 返回一个 BigDecimal其规模是指定的值,其值是数值等于该 BigDecimal的。
BigDecimalsetScale(int newScale, int roundingMode) 返回一个 BigDecimal其规模是规定值,且不成比例的价值乘以或除以 BigDecimal的缩放值十的合适的电源来维持其整体价值的确定。
BigDecimalsetScale(int newScale, RoundingMode roundingMode) 返回一个 BigDecimal其规模是规定值,且不成比例的价值乘以或除以 BigDecimal的缩放值十的合适的电源来维持其整体价值的确定。
shortshortValueExact() 将这一 short BigDecimal,检查丢失的信息。
intsignum() 返回该 BigDecimal signum函数。
BigDecimalstripTrailingZeros() 返回一个 BigDecimal,数值上等于这一但任何尾随零从表示删除。
BigDecimalsubtract(BigDecimal subtrahend) 返回的值是 BigDecimal (this - subtrahend),其规模 max(this.scale(), subtrahend.scale())
BigDecimalsubtract(BigDecimal subtrahend, MathContext mc) 返回的值是 BigDecimal (this - subtrahend),舍入根据语境的设置。
BigIntegertoBigInteger() 将这一 BigInteger BigDecimal
BigIntegertoBigIntegerExact() 将这一 BigInteger BigDecimal,检查丢失的信息。
StringtoEngineeringString()BigDecimal返回字符串表示,如果指数是需要使用工程符号。
StringtoPlainString() 返回一个没有指数域这 BigDecimal字符串表示形式。
StringtoString() 返回该 BigDecimal字符串表示,如果指数是需要使用科学记数法。
BigDecimalulp() 返回一个小的尺寸,最后一个单元,这 BigDecimal
BigIntegerunscaledValue() 返回一个 BigInteger其价值是本 BigDecimal的不成比例的价值。
static BigDecimalvalueOf(double val) 翻译 doubleBigDecimal,使用 double正则字符串表示的 Double.toString(double)方法提供。
static BigDecimalvalueOf(long val) 翻译 long值变成零规模 BigDecimal
static BigDecimalvalueOf(long unscaledVal, int scale) 翻译 long不成比例的价值和 int规模为 BigDecimal
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值