类 java.math.BigDecimal 和java.math.BigInteger的使用

本文详细介绍了Java中的数学类BigInteger和BigDecimal的功能与用法,包括它们的常用方法、构造方法及如何与其他类交互,适合需要进行高精度计算的开发者阅读。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

 

java.mathBigInteger 的使用
 

 

声明为 BigIntegerjava.math 中的字段
static BigInteger BigInteger. ONE
          BigInteger 的常量 1。
static BigInteger BigInteger. TEN
          BigInteger 的常量 10。
static BigInteger BigInteger. ZERO
          BigInteger 的常量 0。
 

 

返回 BigIntegerjava.math 中的方法
 BigInteger BigInteger. abs ()
          返回其值是此 BigInteger 的绝对值的 BigInteger。
 BigInteger BigInteger. add (BigInteger  val)
          返回其值为 (this + val) 的 BigInteger。
 BigInteger BigInteger. and (BigInteger  val)
          返回其值为 (this & val) 的 BigInteger。
 BigInteger BigInteger. andNot (BigInteger  val)
          返回其值为 (this & ~val) 的 BigInteger。
 BigInteger BigInteger. clearBit (int n)
          返回其值与清除了指定位的此 BigInteger 等效的 BigInteger。
 BigInteger BigInteger. divide (BigInteger  val)
          返回其值为 (this / val) 的 BigInteger。
 BigInteger [] BigInteger. divideAndRemainder (BigInteger  val)
          返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
 BigInteger BigInteger. flipBit (int n)
          返回其值与对此 BigInteger 进行指定位翻转后的值等效的 BigInteger。
 BigInteger BigInteger. gcd (BigInteger  val)
          返回一个 BigInteger,其值是 abs(this)abs(val) 的最大公约数。
 BigInteger BigInteger. max (BigInteger  val)
          返回此 BigInteger 和 val 的最大值。
 BigInteger BigInteger. min (BigInteger  val)
          返回此 BigInteger 和 val 的最小值。
 BigInteger BigInteger. mod (BigInteger  m)
          返回其值为 (this mod m ) 的 BigInteger。
 BigInteger BigInteger. modInverse (BigInteger  m)
          返回其值为 (this-1 mod m) 的 BigInteger。
 BigInteger BigInteger. modPow (BigInteger  exponent, BigInteger  m)
          返回其值为 (thisexponent mod m) 的 BigInteger。
 BigInteger BigInteger. multiply (BigInteger  val)
          返回其值为 (this * val) 的 BigInteger。
 BigInteger BigInteger. negate ()
          返回其值是 (-this) 的 BigInteger。
 BigInteger BigInteger. nextProbablePrime ()
          返回大于此 BigInteger 的可能为素数的第一个整数。
 BigInteger BigInteger. not ()
          返回其值为 (~this) 的 BigInteger。
 BigInteger BigInteger. or (BigInteger  val)
          返回其值为 (this | val) 的 BigInteger。
 BigInteger BigInteger. pow (int exponent)
          返回其值为 (thisexponent ) 的 BigInteger。
static BigInteger BigInteger. probablePrime (int bitLength, Random  rnd)
          返回有可能是素数的、具有指定长度的正 BigInteger。
 BigInteger BigInteger. remainder (BigInteger  val)
          返回其值为 (this % val) 的 BigInteger。
 BigInteger BigInteger. setBit (int n)
          返回其值与设置了指定位的此 BigInteger 等效的 BigInteger。
 BigInteger BigInteger. shiftLeft (int n)
          返回其值为 (this << n) 的 BigInteger。
 BigInteger BigInteger. shiftRight (int n)
          返回其值为 (this >> n) 的 BigInteger。
 BigInteger BigInteger. subtract (BigInteger  val)
          返回其值为 (this - val) 的 BigInteger。
 BigInteger BigDecimal. toBigInteger ()
          将此 BigDecimal 转换为 BigInteger
 BigInteger BigDecimal. toBigIntegerExact ()
          将此 BigDecimal 转换为 BigInteger ,以检查丢失的信息。
 BigInteger BigDecimal. unscaledValue ()
          返回其值为此 BigDecimal非标度值BigInteger
static BigInteger BigInteger. valueOf (long val)
          返回其值等于指定 long 的值的 BigInteger。
 BigInteger BigInteger. xor (BigInteger  val)
          返回其值为 (this ^ val) 的 BigInteger。
 

 

参数类型为 BigIntegerjava.math 中的方法
 BigInteger BigInteger. add (BigInteger  val)
          返回其值为 (this + val) 的 BigInteger。
 BigInteger BigInteger. and (BigInteger  val)
          返回其值为 (this & val) 的 BigInteger。
 BigInteger BigInteger. andNot (BigInteger  val)
          返回其值为 (this & ~val) 的 BigInteger。
 int BigInteger. compareTo (BigInteger  val)
          将此 BigInteger 与指定的 BigInteger 进行比较。
 BigInteger BigInteger. divide (BigInteger  val)
          返回其值为 (this / val) 的 BigInteger。
 BigInteger [] BigInteger. divideAndRemainder (BigInteger  val)
          返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
 BigInteger BigInteger. gcd (BigInteger  val)
          返回一个 BigInteger,其值是 abs(this)abs(val) 的最大公约数。
 BigInteger BigInteger. max (BigInteger  val)
          返回此 BigInteger 和 val 的最大值。
 BigInteger BigInteger. min (BigInteger  val)
          返回此 BigInteger 和 val 的最小值。
 BigInteger BigInteger. mod (BigInteger  m)
          返回其值为 (this mod m ) 的 BigInteger。
 BigInteger BigInteger. modInverse (BigInteger  m)
          返回其值为 (this-1 mod m) 的 BigInteger。
 BigInteger BigInteger. modPow (BigInteger  exponent, BigInteger  m)
          返回其值为 (thisexponent mod m) 的 BigInteger。
 BigInteger BigInteger. multiply (BigInteger  val)
          返回其值为 (this * val) 的 BigInteger。
 BigInteger BigInteger. or (BigInteger  val)
          返回其值为 (this | val) 的 BigInteger。
 BigInteger BigInteger. remainder (BigInteger  val)
          返回其值为 (this % val) 的 BigInteger。
 BigInteger BigInteger. subtract (BigInteger  val)
          返回其值为 (this - val) 的 BigInteger。
 BigInteger BigInteger. xor (BigInteger  val)
          返回其值为 (this ^ val) 的 BigInteger。
 

 

参数类型为 BigIntegerjava.math 中的构造方法
BigDecimal (BigInteger  val)
          将 BigInteger 转换为 BigDecimal
BigDecimal (BigInteger  unscaledVal, int scale)
          将 BigInteger 非标度值和 int 标度转换为 BigDecimal
BigDecimal (BigInteger  unscaledVal, int scale, MathContext  mc)
          将 BigInteger 非标度值和 int 标度转换为 BigDecimal (根据上下文设置进行舍入)。
BigDecimal (BigInteger  val, MathContext  mc)
          将 BigInteger 转换为 BigDecimal (根据上下文设置进行舍入)。

  java.utilBigInteger 的使用

 
返回 BigIntegerjava.util 中的方法
 BigInteger Scanner. nextBigInteger ()
          将输入信息的下一个标记扫描为一个 BigInteger
 BigInteger Scanner. nextBigInteger (int radix)
          将输入信息的下一个标记扫描为一个 BigInteger

 

 

 

 

 

 

 

java.mathBigDecimal 的使用
 

 

声明为 BigDecimaljava.math 中的字段
static BigDecimal BigDecimal. ONE
          值为 1,标度为 0。
static BigDecimal BigDecimal. TEN
          值为 10,标度为 0。
static BigDecimal BigDecimal. ZERO
          值为 0,标度为 0。
 

 

返回 BigDecimaljava.math 中的方法
 BigDecimal BigDecimal. abs ()
          返回 BigDecimal ,其值为此 BigDecimal 的绝对值,其标度为 this.scale()
 BigDecimal BigDecimal. abs (MathContext  mc)
          返回其值为此 BigDecimal 绝对值的 BigDecimal (根据上下文设置进行舍入)。
 BigDecimal BigDecimal. add (BigDecimal  augend)
          返回一个 BigDecimal ,其值为 (this + augend) ,其标度为 max(this.scale(), augend.scale())
 BigDecimal BigDecimal. add (BigDecimal  augend, MathContext  mc)
          返回其值为 (this + augend)BigDecimal (根据上下文设置进行舍入)。
 BigDecimal BigDecimal. divide (BigDecimal  divisor)
          返回一个 BigDecimal ,其值为 (this / divisor) ,其首选标度为 (this.scale() - divisor.scale()) ;如果无法表示准确的商值(因为它有无穷的十进制扩展),则抛出 ArithmeticException
 BigDecimal BigDecimal. divide (BigDecimal  divisor, int roundingMode)
          返回一个 BigDecimal ,其值为 (this / divisor) ,其标度为 this.scale()
 BigDecimal BigDecimal. divide (BigDecimal  divisor, int scale, int roundingMode)
          返回一个 BigDecimal ,其值为 (this / divisor) ,其标度为指定标度。
 BigDecimal BigDecimal. divide (BigDecimal  divisor, int scale, RoundingMode  roundingMode)
          返回一个 BigDecimal ,其值为 (this / divisor) ,其标度为指定标度。
 BigDecimal BigDecimal. divide (BigDecimal  divisor, MathContext  mc)
          返回其值为 (this / divisor)BigDecimal (根据上下文设置进行舍入)。
 BigDecimal BigDecimal. divide (BigDecimal  divisor, RoundingMode  roundingMode)
          返回一个 BigDecimal ,其值为 (this / divisor) ,其标度为 this.scale()
 BigDecimal [] BigDecimal. divideAndRemainder (BigDecimal  divisor)
          返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟对两个操作数计算所得到的 remainder
 BigDecimal [] BigDecimal. divideAndRemainder (BigDecimal  divisor, MathContext  mc)
          返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟根据上下文设置对两个操作数进行舍入计算所得到的 remainder 的结果。
 BigDecimal BigDecimal. divideToIntegralValue (BigDecimal  divisor)
          返回 BigDecimal ,其值为向下舍入所得商值 (this / divisor) 的整数部分。
 BigDecimal BigDecimal. divideToIntegralValue (BigDecimal  divisor, MathContext  mc)
          返回 BigDecimal ,其值为 (this / divisor) 的整数部分。
 BigDecimal BigDecimal. max (BigDecimal  val)
          返回此 BigDecimalval 的最大值。
 BigDecimal BigDecimal. min (BigDecimal  val)
          返回此 BigDecimalval 的最小值。
 BigDecimal BigDecimal. movePointLeft (int n)
          返回一个 BigDecimal ,它等效于将该值的小数点向左移动 n 位。
 BigDecimal BigDecimal. movePointRight (int n)
          返回一个 BigDecimal ,它等效于将该值的小数点向右移动 n 位。
 BigDecimal BigDecimal. multiply (BigDecimal  multiplicand)
          返回一个 BigDecimal ,其值为 (this × multiplicand) ,其标度为 (this.scale() + multiplicand.scale())
 BigDecimal BigDecimal. multiply (BigDecimal  multiplicand, MathContext  mc)
          返回其值为 (this × multiplicand)BigDecimal (根据上下文设置进行舍入)。
 BigDecimal BigDecimal. negate ()
          返回 BigDecimal ,其值为 (-this) ,其标度为 this.scale()
 BigDecimal BigDecimal. negate (MathContext  mc)
          返回其值为 (-this)BigDecimal (根据上下文设置进行舍入)。
 BigDecimal BigDecimal. plus ()
          返回 BigDecimal ,其值为 (+this) ,其标度为 this.scale()
 BigDecimal BigDecimal. plus (MathContext  mc)
          返回其值为 (+this)BigDecimal (根据上下文设置进行舍入)。
 BigDecimal BigDecimal. pow (int n)
          返回其值为 (thisn )BigDecimal ,准确计算该幂,使其具有无限精度。
 BigDecimal BigDecimal. pow (int n, MathContext  mc)
          返回其值为 (thisn )BigDecimal
 BigDecimal BigDecimal. remainder (BigDecimal  divisor)
          返回其值为 (this % divisor)BigDecimal
 BigDecimal BigDecimal. remainder (BigDecimal  divisor, MathContext  mc)
          返回其值为 (this % divisor)BigDecimal (根据上下文设置进行舍入)。
 BigDecimal BigDecimal. round (MathContext  mc)
          返回根据 MathContext 设置进行舍入后的 BigDecimal
 BigDecimal BigDecimal. scaleByPowerOfTen (int n)
          返回其数值等于 (this * 10n ) 的 BigDecimal。
 BigDecimal BigDecimal. setScale (int newScale)
          返回一个 BigDecimal ,其标度为指定值,其值在数值上等于此 BigDecimal 的值。
 BigDecimal BigDecimal. setScale (int newScale, int roundingMode)
          返回一个 BigDecimal ,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。
 BigDecimal BigDecimal. setScale (int newScale, RoundingMode  roundingMode)
          返回 BigDecimal ,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。
 BigDecimal BigDecimal. stripTrailingZeros ()
          返回数值上等于此小数,但从该表示形式移除所有尾部零的 BigDecimal
 BigDecimal BigDecimal. subtract (BigDecimal  subtrahend)
          返回一个 BigDecimal ,其值为 (this - subtrahend) ,其标度为 max(this.scale(), subtrahend.scale())
 BigDecimal BigDecimal. subtract (BigDecimal  subtrahend, MathContext  mc)
          返回其值为 (this - subtrahend)BigDecimal (根据上下文设置进行舍入)。
 BigDecimal BigDecimal. ulp ()
          返回此 BigDecimal 的 ulp(最后一位的单位)的大小。
static BigDecimal BigDecimal. valueOf (double val)
          使用 Double.toString(double) 方法提供的 double 规范的字符串表示形式将 double 转换为 BigDecimal
static BigDecimal BigDecimal. valueOf (long val)
          将 long 值转换为具有零标度的 BigDecimal
static BigDecimal BigDecimal. valueOf (long unscaledVal, int scale)
          将 long 非标度值和 int 标度转换为 BigDecimal
 

 

参数类型为 BigDecimaljava.math 中的方法
 BigDecimal BigDecimal. add (BigDecimal  augend)
          返回一个 BigDecimal ,其值为 (this + augend) ,其标度为 max(this.scale(), augend.scale())
 BigDecimal BigDecimal. add (BigDecimal  augend, MathContext  mc)
          返回其值为 (this + augend)BigDecimal (根据上下文设置进行舍入)。
 int BigDecimal. compareTo (BigDecimal  val)
          将此 BigDecimal 与指定的 BigDecimal 比较。
 BigDecimal BigDecimal. divide (BigDecimal  divisor)
          返回一个 BigDecimal ,其值为 (this / divisor) ,其首选标度为 (this.scale() - divisor.scale()) ;如果无法表示准确的商值(因为它有无穷的十进制扩展),则抛出 ArithmeticException
 BigDecimal BigDecimal. divide (BigDecimal  divisor, int roundingMode)
          返回一个 BigDecimal ,其值为 (this / divisor) ,其标度为 this.scale()
 BigDecimal BigDecimal. divide (BigDecimal  divisor, int scale, int roundingMode)
          返回一个 BigDecimal ,其值为 (this / divisor) ,其标度为指定标度。
 BigDecimal BigDecimal. divide (BigDecimal  divisor, int scale, RoundingMode  roundingMode)
          返回一个 BigDecimal ,其值为 (this / divisor) ,其标度为指定标度。
 BigDecimal BigDecimal. divide (BigDecimal  divisor, MathContext  mc)
          返回其值为 (this / divisor)BigDecimal (根据上下文设置进行舍入)。
 BigDecimal BigDecimal. divide (BigDecimal  divisor, RoundingMode  roundingMode)
          返回一个 BigDecimal ,其值为 (this / divisor) ,其标度为 this.scale()
 BigDecimal [] BigDecimal. divideAndRemainder (BigDecimal  divisor)
          返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟对两个操作数计算所得到的 remainder
 BigDecimal [] BigDecimal. divideAndRemainder (BigDecimal  divisor, MathContext  mc)
          返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟根据上下文设置对两个操作数进行舍入计算所得到的 remainder 的结果。
 BigDecimal BigDecimal. divideToIntegralValue (BigDecimal  divisor)
          返回 BigDecimal ,其值为向下舍入所得商值 (this / divisor) 的整数部分。
 BigDecimal BigDecimal. divideToIntegralValue (BigDecimal  divisor, MathContext  mc)
          返回 BigDecimal ,其值为 (this / divisor) 的整数部分。
 BigDecimal BigDecimal. max (BigDecimal  val)
          返回此 BigDecimalval 的最大值。
 BigDecimal BigDecimal. min (BigDecimal  val)
          返回此 BigDecimalval 的最小值。
 BigDecimal BigDecimal. multiply (BigDecimal  multiplicand)
          返回一个 BigDecimal ,其值为 (this × multiplicand) ,其标度为 (this.scale() + multiplicand.scale())
 BigDecimal BigDecimal. multiply (BigDecimal  multiplicand, MathContext  mc)
          返回其值为 (this × multiplicand)BigDecimal (根据上下文设置进行舍入)。
 BigDecimal BigDecimal. remainder (BigDecimal  divisor)
          返回其值为 (this % divisor)BigDecimal
 BigDecimal BigDecimal. remainder (BigDecimal  divisor, MathContext  mc)
          返回其值为 (this % divisor)BigDecimal (根据上下文设置进行舍入)。
 BigDecimal BigDecimal. subtract (BigDecimal  subtrahend)
          返回一个 BigDecimal ,其值为 (this - subtrahend) ,其标度为 max(this.scale(), subtrahend.scale())
 BigDecimal BigDecimal. subtract (BigDecimal  subtrahend, MathContext  mc)
          返回其值为 (this - subtrahend)BigDecimal (根据上下文设置进行舍入)。

  java.utilBigDecimal 的使用

 

 

返回 BigDecimaljava.util 中的方法
 BigDecimal Scanner. nextBigDecimal ()
          将输入信息的下一个标记扫描为一个 BigDecimal

内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年3月1日至2019年3月26日共391天的数据作为样本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如样本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值