java中biginteger和bigdecimal在大数计算中的使用

2024-08-28 21:58

本文主要是介绍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具体方法请打开链接
 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 的最小的二进制补码表示形式的位数,不包括 符号位。
 BigIntegerclearBit(int n)
          返回其值与清除了指定位的此 BigInteger 等效的 BigInteger。
 intcompareTo(BigInteger val)
          将此 BigInteger 与指定的 BigInteger 进行比较。
 BigIntegerdivide(BigInteger val)
          返回其值为 (this / val) 的 BigInteger。
 BigInteger[]divideAndRemainder(BigInteger val)
          返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
 doubledoubleValue()
          将此 BigInteger 转换为 double
 booleanequals(Object x)
          比较此 BigInteger 与指定的 Object 的相等性。
 BigIntegerflipBit(int n)
          返回其值与对此 BigInteger 进行指定位翻转后的值等效的 BigInteger。
 floatfloatValue()
          将此 BigInteger 转换为 float
 BigIntegergcd(BigInteger val)
          返回一个 BigInteger,其值是 abs(this)abs(val) 的最大公约数。
 intgetLowestSetBit()
          返回此 BigInteger 最右端(最低位)1 比特的索引(即从此字节的右端开始到本字节中最右端 1 比特之间的 0 比特的位数)。
 inthashCode()
          返回此 BigInteger 的哈希码。
 intintValue()
          将此 BigInteger 转换为 int
 booleanisProbablePrime(int certainty)
          如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false
 longlongValue()
          将此 BigInteger 转换为 long
 BigIntegermax(BigInteger val)
          返回此 BigInteger 和 val 的最大值。
 BigIntegermin(BigInteger val)
          返回此 BigInteger 和 val 的最小值。
 BigIntegermod(BigInteger m)
          返回其值为 (this mod m) 的 BigInteger。
 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 | val) 的 BigInteger。
 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。
 intsignum()
          返回此 BigInteger 的正负号函数。
 BigIntegersubtract(BigInteger val)
          返回其值为 (this - val) 的 BigInteger。
 booleantestBit(int n)
          当且仅当设置了指定的位时,返回 true
 byte[]toByteArray()
          返回一个 byte 数组,该数组包含此 BigInteger 的二进制补码表示形式。
 StringtoString()
          返回此 BigInteger 的十进制字符串表示形式。
 StringtoString(int radix)
          返回此 BigInteger 的给定基数的字符串表示形式。
static BigIntegervalueOf(long val)
          返回其值等于指定 long 的值的 BigInteger。
 BigIntegerxor(BigInteger val)
          返回其值为 (this ^ val) 的 BigInteger。
我们可以利用BigInteger中的方法实现赋值,加减乘除,取模等各种运算,java中并没有重载运算符,所以不可以使用运算操作符,要使用函数。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+bSystem.out.println(c);c=a.multiply(b);//计算a*bSystem.out.println(c);c=a.subtract(b);//计算a-bSystem.out.println(c);c=a.divide(b);//计算a/bSystem.out.println(c);c=a.mod(b);//计算a%bSystem.out.println(c);}
cin.close();}}
就是对java中类的方法的使用,细心认真就可以
2BigDecimal的使用
BigDecimal的一些基本方法,具体参考http://www.apihome.cn/api/java/BigDecimal.html

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)
          返回其值为 (this + augend)BigDecimal(根据上下文设置进行舍入)。
 bytebyteValueExact()
          将此 BigDecimal 转换为 byte,以检查丢失的信息。
 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)
          返回其值为 (this / divisor)BigDecimal(根据上下文设置进行舍入)。
 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()
          将此 BigDecimal 转换为 double
 booleanequals(Object x)
          比较此 BigDecimal 与指定的 Object 的相等性。
 floatfloatValue()
          将此 BigDecimal 转换为 float
 inthashCode()
          返回此 BigDecimal 的哈希码。
 intintValue()
          将此 BigDecimal 转换为 int
 intintValueExact()
          将此 BigDecimal 转换为 int,以检查丢失的信息。
 longlongValue()
          将此 BigDecimal 转换为 long
 longlongValueExact()
          将此 BigDecimal 转换为 long,以检查丢失的信息。
 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)
          返回其值为 (this × multiplicand)BigDecimal(根据上下文设置进行舍入)。
 BigDecimalnegate()
          返回 BigDecimal,其值为 (-this),其标度为 this.scale()
 BigDecimalnegate(MathContext mc)
          返回其值为 (-this)BigDecimal(根据上下文设置进行舍入)。
 BigDecimalplus()
          返回 BigDecimal,其值为 (+this),其标度为 this.scale()
 BigDecimalplus(MathContext mc)
          返回其值为 (+this)BigDecimal(根据上下文设置进行舍入)。
 BigDecimalpow(int n)
          返回其值为 (thisn)BigDecimal,准确计算该幂,使其具有无限精度。
 BigDecimalpow(int n, MathContext mc)
          返回其值为 (thisn)BigDecimal
 intprecision()
          返回此 BigDecimal精度
 BigDecimalremainder(BigDecimal divisor)
          返回其值为 (this % divisor)BigDecimal
 BigDecimalremainder(BigDecimal divisor, MathContext mc)
          返回其值为 (this % divisor)BigDecimal(根据上下文设置进行舍入)。
 BigDecimalround(MathContext mc)
          返回根据 MathContext 设置进行舍入后的 BigDecimal
 intscale()
          返回此 BigDecimal标度
 BigDecimalscaleByPowerOfTen(int n)
          返回其数值等于 (this * 10n) 的 BigDecimal。
 BigDecimalsetScale(int newScale)
          返回一个 BigDecimal,其标度为指定值,其值在数值上等于此 BigDecimal 的值。
 BigDecimalsetScale(int newScale, int roundingMode)
          返回一个 BigDecimal,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。
 BigDecimalsetScale(int newScale, RoundingMode roundingMode)
          返回 BigDecimal,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。
 shortshortValueExact()
          将此 BigDecimal 转换为 short,以检查丢失的信息。
 intsignum()
          返回此 BigDecimal 的正负号函数。
 BigDecimalstripTrailingZeros()
          返回数值上等于此小数,但从该表示形式移除所有尾部零的 BigDecimal
 BigDecimalsubtract(BigDecimal subtrahend)
          返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())
 BigDecimalsubtract(BigDecimal subtrahend, MathContext mc)
          返回其值为 (this - subtrahend)BigDecimal(根据上下文设置进行舍入)。
 BigIntegertoBigInteger()
          将此 BigDecimal 转换为 BigInteger
 BigIntegertoBigIntegerExact()
          将此 BigDecimal 转换为 BigInteger,以检查丢失的信息。
 StringtoEngineeringString()
          返回此 BigDecimal 的字符串表示形式,需要指数时,则使用工程计数法。
 StringtoPlainString()
          返回不带指数字段的此 BigDecimal 的字符串表示形式。
 StringtoString()
          返回此 BigDecimal 的字符串表示形式,如果需要指数,则使用科学记数法。
 BigDecimalulp()
          返回此 BigDecimal 的 ulp(最后一位的单位)的大小。
 BigIntegerunscaledValue()
          返回其值为此 BigDecimal非标度值BigInteger
static BigDecimalvalueOf(double val)
          使用 Double.toString(double) 方法提供的 double 规范的字符串表示形式将 double 转换为BigDecimal
static BigDecimalvalueOf(long val)
          将 long 值转换为具有零标度的 BigDecimal
static BigDecimalvalueOf(long unscaledVal, int scale)
          将 long 非标度值和 int 标度转换为 BigDecimal
  BIgDecimal和BigInteger的基本使用方法差不多,要注意消除尾0,和转化为非指数型字符串输出
基本使用代码如下
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.000009System.out.println(c);//0.000010System.out.println(c.stripTrailingZeros());//消尾0输出//结果抛出异常0.00001//可以的使用MathContext()控制输出精度System.out.println(c.stripTrailingZeros().toPlainString());//消尾0转化为非指数型字符串输出,等于没舍入的完整精度  //0.00001c=a.multiply(b);//计算a*bSystem.out.println(c);c=a.subtract(b);//计算a-bSystem.out.println(c);c=a.divide(b, new MathContext(3));//计算a/控制保留三位有效数字System.out.println(c);}
cin.close();}}

以上就是java大数的基本用法,遇到具体问题再进行总结

























这篇关于java中biginteger和bigdecimal在大数计算中的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1115997

相关文章

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4