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

简介: 记录Java中的大数

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 Type Method and Description
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的比特数,不包括符号位。
byte byteValueExact() 将这一 byte 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)数组。
double doubleValue() 将这一 double BigInteger。
boolean equals(Object x) 这与平等BigInteger指定对象比较。
BigInteger flipBit(int n) 返回一个BigInteger其值等效于这个BigInteger与指定的位翻转。
float floatValue() 将这一 float BigInteger。
BigInteger gcd(BigInteger val) 返回的值是 abs(this) BigInteger和 abs(val)最大公约数。
int getLowestSetBit() 返回最右边的指数(低级)在BigInteger这一点(在最右边的一位右零比特数)。
int hashCode() 返回此BigInteger的哈希代码。
int intValue() BigInteger将这一 int
int intValueExact() 将这一 int BigInteger,检查丢失的信息。
boolean isProbablePrime(int certainty) 返回 true如果这很可能就是BigInteger总理, false如果它肯定复合。
long longValue() 将这一 long BigInteger。
long longValueExact() 将这一 long BigInteger,检查丢失的信息。
BigInteger max(BigInteger val) 返回此BigInteger和 val最大。
BigInteger min(BigInteger val) 返回此BigInteger和 val最小。
BigInteger mod(BigInteger m) 返回一个BigInteger的价值 (this mod m)。
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。
short shortValueExact() 将这一 short BigInteger,检查丢失的信息。
int signum() 返回此BigInteger的符号函数。
BigInteger subtract(BigInteger val) 返回的值是 (this - val) BigInteger。
boolean testBit(int n) 返回 true当且仅当指定的位设置。
byte[] toByteArray() 返回一个包含此BigInteger的二进制补码表示的字节数组。
String toString() 返回此BigInteger十进制字符串表示形式。
String toString(int radix) 返回在给定的基本BigInteger的字符串表示形式。
static BigInteger valueOf(long val) 返回一个BigInteger其值等于指定的 long
BigInteger xor(BigInteger val) 返回的值是 (this ^ val) BigInteger。

BigDecimal

Modifier and Type Method and Description
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) 返回的值是 BigDecimal (this + augend),舍入根据语境的设置。
byte byteValueExact() 将这一 byte BigDecimal,检查丢失的信息。
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) 返回的值是 BigDecimal (this / divisor),舍入根据语境的设置。
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() 将这一 double BigDecimal
boolean equals(Object x)BigDecimal与平等的规定 Object比较。
float floatValue() 将这一 float BigDecimal
int hashCode() 返回此 BigDecimal哈希代码。
int intValue() 将这一 int BigDecimal
int intValueExact() 将这一 int BigDecimal,检查丢失的信息。
long longValue() 将这一 long BigDecimal
long longValueExact() 将这一 long BigDecimal,检查丢失的信息。
BigDecimal max(BigDecimal val) 返回该 BigDecimalval最大。
BigDecimal min(BigDecimal val) 返回该 BigDecimalval最小。
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) 返回的值是 BigDecimal (this × multiplicand),舍入根据语境的设置。
BigDecimal negate() 返回的值是 BigDecimal (-this),其规模 this.scale()
BigDecimal negate(MathContext mc) 返回的值是 BigDecimal (-this),舍入根据语境的设置。
BigDecimal plus() 返回的值是 BigDecimal (+this),其规模 this.scale()
BigDecimal plus(MathContext mc) 返回的值是 BigDecimal (+this),舍入根据语境的设置。
BigDecimal pow(int n) 返回的值是 BigDecimal (thisn),权力是精确计算,无限精度。
BigDecimal pow(int n, MathContext mc) 返回的值是 (thisn) BigDecimal
int precision() 返回该 BigDecimal的精度。
BigDecimal remainder(BigDecimal divisor) 返回的值是 (this % divisor) BigDecimal
BigDecimal remainder(BigDecimal divisor, MathContext mc) 返回的值是 BigDecimal (this % divisor),舍入根据语境的设置。
BigDecimal round(MathContext mc) 返回一个 BigDecimal圆形根据 MathContext设置。
int scale() 返回该 BigDecimal的规模。
BigDecimal scaleByPowerOfTen(int n) 返回来的值等于( this×10 n)。
BigDecimal setScale(int newScale) 返回一个 BigDecimal其规模是指定的值,其值是数值等于该 BigDecimal的。
BigDecimal setScale(int newScale, int roundingMode) 返回一个 BigDecimal其规模是规定值,且不成比例的价值乘以或除以 BigDecimal的缩放值十的合适的电源来维持其整体价值的确定。
BigDecimal setScale(int newScale, RoundingMode roundingMode) 返回一个 BigDecimal其规模是规定值,且不成比例的价值乘以或除以 BigDecimal的缩放值十的合适的电源来维持其整体价值的确定。
short shortValueExact() 将这一 short BigDecimal,检查丢失的信息。
int signum() 返回该 BigDecimal signum函数。
BigDecimal stripTrailingZeros() 返回一个 BigDecimal,数值上等于这一但任何尾随零从表示删除。
BigDecimal subtract(BigDecimal subtrahend) 返回的值是 BigDecimal (this - subtrahend),其规模 max(this.scale(), subtrahend.scale())
BigDecimal subtract(BigDecimal subtrahend, MathContext mc) 返回的值是 BigDecimal (this - subtrahend),舍入根据语境的设置。
BigInteger toBigInteger() 将这一 BigInteger BigDecimal
BigInteger toBigIntegerExact() 将这一 BigInteger BigDecimal,检查丢失的信息。
String toEngineeringString()BigDecimal返回字符串表示,如果指数是需要使用工程符号。
String toPlainString() 返回一个没有指数域这 BigDecimal字符串表示形式。
String toString() 返回该 BigDecimal字符串表示,如果指数是需要使用科学记数法。
BigDecimal ulp() 返回一个小的尺寸,最后一个单元,这 BigDecimal
BigInteger unscaledValue() 返回一个 BigInteger其价值是本 BigDecimal的不成比例的价值。
static BigDecimal valueOf(double val) 翻译 doubleBigDecimal,使用 double正则字符串表示的 Double.toString(double)方法提供。
static BigDecimal valueOf(long val) 翻译 long值变成零规模 BigDecimal
static BigDecimal valueOf(long unscaledVal, int scale) 翻译 long不成比例的价值和 int规模为 BigDecimal
相关文章
|
存储 监控 数据可视化
日志分析对决:揭示 ELK 与 GrayLog 的优势和差异
日志分析对决:揭示 ELK 与 GrayLog 的优势和差异
3336 0
|
网络安全
编译原理复习二:Top-Down分析LL(1)文法的判断与LL(1)分析表的构造(附题目与答案 超详细)
编译原理复习二:Top-Down分析LL(1)文法的判断与LL(1)分析表的构造(附题目与答案 超详细)
909 1
|
Ubuntu
Ubuntu 18.04中卸载及安装G2O库
Ubuntu 18.04中卸载及安装G2O库
1005 0
|
小程序 API 开发者
【小程序全面解析】生命周期、常用组件,代码示例和使用场景
该文章全面介绍了小程序的生命周期、常用基础组件以及使用场景,并提供了相应的代码示例。读者可以了解小程序的生命周期函数及其执行时机,以及学习如何使用常用基础组件构建小程序页面。此外,文章还列举了各种使用场景,帮助读者更好地理解如何应用小程序开发。如果您是小程序开发的初学者或需要了解小程序的基础知识,该文章将为您提供全面的帮助和指导。
646 0
【小程序全面解析】生命周期、常用组件,代码示例和使用场景
|
存储 Java
Java 中 ArrayList 的初始大小是多少?
【8月更文挑战第23天】
428 0
【IntelliJ IDEA】idea 收起注释、打开注释、隐藏注释的快捷键
【IntelliJ IDEA】idea 收起注释、打开注释、隐藏注释的快捷键
1185 0
|
机器学习/深度学习 算法 数据安全/隐私保护
图像滤镜艺术---人脸编辑(五官微调+瘦脸美型)
原文:图像滤镜艺术---人脸编辑(五官微调+瘦脸美型) 写本文的目的,实际上是对目前人脸美型这一块技术做个总结,跟大家 分享一下!目前提到美颜算法,大家都会想到磨皮美白 /大眼瘦脸,实际上做好 美颜这件事情,关乎的不仅仅是这些,还有五官的协调比例等,今天我们主要说一下五官的微调,这里我直接称之为人脸编辑吧。
3716 0
|
关系型数据库 MySQL Linux
jeecg-boot切换数据库mysql到postgresql报错:org.postgresql.util.PSQLException: 错误: 关系 “sched_name“ 不存在
jeecg-boot切换数据库mysql到postgresql报错:org.postgresql.util.PSQLException: 错误: 关系 “sched_name“ 不存在
570 0
|
机器学习/深度学习 传感器 算法
【BP回归预测】基于粒子群算法PSO优化BP神经网络实现预测多输入多输出附matlab代码
【BP回归预测】基于粒子群算法PSO优化BP神经网络实现预测多输入多输出附matlab代码