java中biginteger和bigdecimal在大数计算中的使用
java中的BigInteger和BigIntegerDecimal
当我们在做Acm的大数题时,我们会发现int,double,表示的范围有限,不能够满足要求,对于c/c++而言,我们就只能采用数组模拟的方法来实现高精度大数的操作。然而java的jdk1.5后就可以使用math包中的BigInteger和BigDecimal来帮助我们解决高精度大数和小数的问题。
1 BigInteger高精度整数的使用。下面给出一些BigInteger的函数方法
ps:参考http://www.apihome.cn/api/java/BigInteger.html具体方法请打开链接
我们可以利用BigInteger中的方法实现赋值,加减乘除,取模等各种运算,java中并没有重载运算符,所以不可以使用运算操作符,要使用函数。BigInteger还提供了三个常量
BigInteger.one 大数1
BigInteger.zero 大数0
BigInteger.ten 大数10
下面是是实现BigInteger基本功能的代码
2BigDecimal的使用
BigDecimal的一些基本方法,具体参考http://www.apihome.cn/api/java/BigDecimal.html
BIgDecimal和BigInteger的基本使用方法差不多,要注意消除尾0,和转化为非指数型字符串输出
基本使用代码如下
以上就是java大数的基本用法,遇到具体问题再进行总结
当我们在做Acm的大数题时,我们会发现int,double,表示的范围有限,不能够满足要求,对于c/c++而言,我们就只能采用数组模拟的方法来实现高精度大数的操作。然而java的jdk1.5后就可以使用math包中的BigInteger和BigDecimal来帮助我们解决高精度大数和小数的问题。
1 BigInteger高精度整数的使用。下面给出一些BigInteger的函数方法
ps:参考http://www.apihome.cn/api/java/BigInteger.html具体方法请打开链接
BigInteger |
abs()返回其值是此 BigInteger 的绝对值的 BigInteger。 |
BigInteger |
add(BigInteger val)返回其值为 (this + val) 的 BigInteger。 |
BigInteger |
and(BigInteger val)返回其值为 (this & val) 的 BigInteger。 |
BigInteger |
andNot(BigInteger val)返回其值为 (this & ~val) 的 BigInteger。 |
int |
bitCount()返回此 BigInteger 的二进制补码表示形式中与符号不同的位的数量。 |
int |
bitLength()返回此 BigInteger 的最小的二进制补码表示形式的位数,不包括 符号位。 |
BigInteger |
clearBit(int n)返回其值与清除了指定位的此 BigInteger 等效的 BigInteger。 |
int |
compareTo(BigInteger val)将此 BigInteger 与指定的 BigInteger 进行比较。 |
BigInteger |
divide(BigInteger val)返回其值为 (this / val) 的 BigInteger。 |
BigInteger[] |
divideAndRemainder(BigInteger val)返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。 |
double |
doubleValue()将此 BigInteger 转换为 double。 |
boolean |
equals(Object x)比较此 BigInteger 与指定的 Object 的相等性。 |
BigInteger |
flipBit(int n)返回其值与对此 BigInteger 进行指定位翻转后的值等效的 BigInteger。 |
float |
floatValue()将此 BigInteger 转换为 float。 |
BigInteger |
gcd(BigInteger val)返回一个 BigInteger,其值是 abs(this) 和 abs(val) 的最大公约数。 |
int |
getLowestSetBit()返回此 BigInteger 最右端(最低位)1 比特的索引(即从此字节的右端开始到本字节中最右端 1 比特之间的 0 比特的位数)。 |
int |
hashCode()返回此 BigInteger 的哈希码。 |
int |
intValue()将此 BigInteger 转换为 int。 |
boolean |
isProbablePrime(int certainty)如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false。 |
long |
longValue()将此 BigInteger 转换为 long。 |
BigInteger |
max(BigInteger val)返回此 BigInteger 和 val 的最大值。 |
BigInteger |
min(BigInteger val)返回此 BigInteger 和 val 的最小值。 |
BigInteger |
mod(BigInteger m)返回其值为 (this mod m) 的 BigInteger。 |
BigInteger |
modInverse(BigInteger m)返回其值为 (this-1 mod m) 的 BigInteger。 |
BigInteger |
modPow(BigInteger exponent, BigInteger m)返回其值为 (thisexponent mod m) 的 BigInteger。 |
BigInteger |
multiply(BigInteger val)返回其值为 (this * val) 的 BigInteger。 |
BigInteger |
negate()返回其值是 (-this) 的 BigInteger。 |
BigInteger |
nextProbablePrime()返回大于此 BigInteger 的可能为素数的第一个整数。 |
BigInteger |
not()返回其值为 (~this) 的 BigInteger。 |
BigInteger |
or(BigInteger val)返回其值为 (this | val) 的 BigInteger。 |
BigInteger |
pow(int exponent)返回其值为 (thisexponent) 的 BigInteger。 |
static BigInteger |
probablePrime(int bitLength, Random rnd)返回有可能是素数的、具有指定长度的正 BigInteger。 |
BigInteger |
remainder(BigInteger val)返回其值为 (this % val) 的 BigInteger。 |
BigInteger |
setBit(int n)返回其值与设置了指定位的此 BigInteger 等效的 BigInteger。 |
BigInteger |
shiftLeft(int n)返回其值为 (this << n) 的 BigInteger。 |
BigInteger |
shiftRight(int n)返回其值为 (this >> n) 的 BigInteger。 |
int |
signum()返回此 BigInteger 的正负号函数。 |
BigInteger |
subtract(BigInteger val)返回其值为 (this - val) 的 BigInteger。 |
boolean |
testBit(int n)当且仅当设置了指定的位时,返回 true。 |
byte[] |
toByteArray()返回一个 byte 数组,该数组包含此 BigInteger 的二进制补码表示形式。 |
String |
toString()返回此 BigInteger 的十进制字符串表示形式。 |
String |
toString(int radix)返回此 BigInteger 的给定基数的字符串表示形式。 |
static BigInteger |
valueOf(long val)返回其值等于指定 long 的值的 BigInteger。 |
BigInteger |
xor(BigInteger val)返回其值为 (this ^ val) 的 BigInteger。 |
BigInteger.one 大数1
BigInteger.zero 大数0
BigInteger.ten 大数10
下面是是实现BigInteger基本功能的代码
import java.util.*;
import java.math.*;
public class jichu {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
BigInteger a,b,c;
while(cin.hasNextBigInteger())
{
a=cin.nextBigInteger();
b=cin.nextBigInteger();
c=a.add(b);//计算a+b
System.out.println(c);
c=a.multiply(b);//计算a*b
System.out.println(c);
c=a.subtract(b);//计算a-b
System.out.println(c);
c=a.divide(b);//计算a/b
System.out.println(c);
c=a.mod(b);//计算a%b
System.out.println(c);
}
cin.close();
}
}
就是对java中类的方法的使用,细心认真就可以2BigDecimal的使用
BigDecimal的一些基本方法,具体参考http://www.apihome.cn/api/java/BigDecimal.html
BigDecimal |
abs()返回 BigDecimal,其值为此 BigDecimal 的绝对值,其标度为 this.scale()。 |
BigDecimal |
abs(MathContext mc)返回其值为此 BigDecimal 绝对值的 BigDecimal(根据上下文设置进行舍入)。 |
BigDecimal |
add(BigDecimal augend)返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。 |
BigDecimal |
add(BigDecimal augend, MathContext mc)返回其值为 (this + augend) 的 BigDecimal(根据上下文设置进行舍入)。 |
byte |
byteValueExact()将此 BigDecimal 转换为 byte,以检查丢失的信息。 |
int |
compareTo(BigDecimal val)将此 BigDecimal 与指定的 BigDecimal 比较。 |
BigDecimal |
divide(BigDecimal divisor)返回一个 BigDecimal,其值为 (this / divisor),其首选标度为 (this.scale() - divisor.scale());如果无法表示准确的商值(因为它有无穷的十进制扩展),则抛出ArithmeticException。 |
BigDecimal |
divide(BigDecimal divisor, int roundingMode)返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。 |
BigDecimal |
divide(BigDecimal divisor, int scale, int roundingMode)返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。 |
BigDecimal |
divide(BigDecimal divisor, int scale, RoundingMode roundingMode)返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。 |
BigDecimal |
divide(BigDecimal divisor, MathContext mc)返回其值为 (this / divisor) 的 BigDecimal(根据上下文设置进行舍入)。 |
BigDecimal |
divide(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 的结果。 |
BigDecimal |
divideToIntegralValue(BigDecimal divisor)返回 BigDecimal,其值为向下舍入所得商值 (this / divisor) 的整数部分。 |
BigDecimal |
divideToIntegralValue(BigDecimal divisor, MathContext mc)返回 BigDecimal,其值为 (this / divisor) 的整数部分。 |
double |
doubleValue()将此 BigDecimal 转换为 double。 |
boolean |
equals(Object x)比较此 BigDecimal 与指定的 Object 的相等性。 |
float |
floatValue()将此 BigDecimal 转换为 float。 |
int |
hashCode()返回此 BigDecimal 的哈希码。 |
int |
intValue()将此 BigDecimal 转换为 int。 |
int |
intValueExact()将此 BigDecimal 转换为 int,以检查丢失的信息。 |
long |
longValue()将此 BigDecimal 转换为 long。 |
long |
longValueExact()将此 BigDecimal 转换为 long,以检查丢失的信息。 |
BigDecimal |
max(BigDecimal val)返回此 BigDecimal 和 val 的最大值。 |
BigDecimal |
min(BigDecimal val)返回此 BigDecimal 和 val 的最小值。 |
BigDecimal |
movePointLeft(int n)返回一个 BigDecimal,它等效于将该值的小数点向左移动 n 位。 |
BigDecimal |
movePointRight(int n)返回一个 BigDecimal,它等效于将该值的小数点向右移动 n 位。 |
BigDecimal |
multiply(BigDecimal multiplicand)返回一个 BigDecimal,其值为 (this × multiplicand),其标度为 (this.scale() + multiplicand.scale())。 |
BigDecimal |
multiply(BigDecimal multiplicand, MathContext mc)返回其值为 (this × multiplicand) 的 BigDecimal(根据上下文设置进行舍入)。 |
BigDecimal |
negate()返回 BigDecimal,其值为 (-this),其标度为 this.scale()。 |
BigDecimal |
negate(MathContext mc)返回其值为 (-this) 的 BigDecimal(根据上下文设置进行舍入)。 |
BigDecimal |
plus()返回 BigDecimal,其值为 (+this),其标度为 this.scale()。 |
BigDecimal |
plus(MathContext mc)返回其值为 (+this) 的 BigDecimal(根据上下文设置进行舍入)。 |
BigDecimal |
pow(int n)返回其值为 (thisn) 的 BigDecimal,准确计算该幂,使其具有无限精度。 |
BigDecimal |
pow(int n, MathContext mc)返回其值为 (thisn) 的 BigDecimal。 |
int |
precision()返回此 BigDecimal 的精度。 |
BigDecimal |
remainder(BigDecimal divisor)返回其值为 (this % divisor) 的 BigDecimal。 |
BigDecimal |
remainder(BigDecimal divisor, MathContext mc)返回其值为 (this % divisor) 的 BigDecimal(根据上下文设置进行舍入)。 |
BigDecimal |
round(MathContext mc)返回根据 MathContext 设置进行舍入后的 BigDecimal。 |
int |
scale()返回此 BigDecimal 的标度。 |
BigDecimal |
scaleByPowerOfTen(int n)返回其数值等于 (this * 10n) 的 BigDecimal。 |
BigDecimal |
setScale(int newScale)返回一个 BigDecimal,其标度为指定值,其值在数值上等于此 BigDecimal 的值。 |
BigDecimal |
setScale(int newScale, int roundingMode)返回一个 BigDecimal,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。 |
BigDecimal |
setScale(int newScale, RoundingMode roundingMode)返回 BigDecimal,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。 |
short |
shortValueExact()将此 BigDecimal 转换为 short,以检查丢失的信息。 |
int |
signum()返回此 BigDecimal 的正负号函数。 |
BigDecimal |
stripTrailingZeros()返回数值上等于此小数,但从该表示形式移除所有尾部零的 BigDecimal。 |
BigDecimal |
subtract(BigDecimal subtrahend)返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。 |
BigDecimal |
subtract(BigDecimal subtrahend, MathContext mc)返回其值为 (this - subtrahend) 的 BigDecimal(根据上下文设置进行舍入)。 |
BigInteger |
toBigInteger()将此 BigDecimal 转换为 BigInteger。 |
BigInteger |
toBigIntegerExact()将此 BigDecimal 转换为 BigInteger,以检查丢失的信息。 |
String |
toEngineeringString()返回此 BigDecimal 的字符串表示形式,需要指数时,则使用工程计数法。 |
String |
toPlainString()返回不带指数字段的此 BigDecimal 的字符串表示形式。 |
String |
toString()返回此 BigDecimal 的字符串表示形式,如果需要指数,则使用科学记数法。 |
BigDecimal |
ulp()返回此 BigDecimal 的 ulp(最后一位的单位)的大小。 |
BigInteger |
unscaledValue()返回其值为此 BigDecimal 的非标度值 的 BigInteger。 |
static BigDecimal |
valueOf(double val)使用 Double.toString(double) 方法提供的 double 规范的字符串表示形式将 double 转换为BigDecimal。 |
static BigDecimal |
valueOf(long val)将 long 值转换为具有零标度的 BigDecimal。 |
static BigDecimal |
valueOf(long unscaledVal, int scale)将 long 非标度值和 int 标度转换为 BigDecimal。 |
基本使用代码如下
import java.util.*;
import java.math.*;
public class jichu {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
BigDecimal a,b,c;
while(cin.hasNextBigDecimal())
{
a=cin.nextBigDecimal();
b=cin.nextBigDecimal();
c=a.add(b);//计算a+b//0.000001 0.000009
System.out.println(c);//0.000010
System.out.println(c.stripTrailingZeros());//消尾0输出//结果抛出异常0.00001//可以的使用MathContext()控制输出精度
System.out.println(c.stripTrailingZeros().toPlainString());//消尾0转化为非指数型字符串输出,等于没舍入的完整精度 //0.00001
c=a.multiply(b);//计算a*b
System.out.println(c);
c=a.subtract(b);//计算a-b
System.out.println(c);
c=a.divide(b, new MathContext(3));//计算a/控制保留三位有效数字
System.out.println(c);
}
cin.close();
}
}
以上就是java大数的基本用法,遇到具体问题再进行总结
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了
