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

简介: 记录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
相关文章
|
14天前
|
人工智能 缓存 安全
LangChain v1.0 中间件详解:彻底搞定 AI Agent 上下文控制
LangChain v1.0 引入中间件机制,系统化解决上下文管理难题。通过模块化中间件,实现输入预处理、敏感信息过滤、工具权限控制等,提升Agent在生产环境的稳定性与可维护性。
279 5
LangChain v1.0 中间件详解:彻底搞定 AI Agent 上下文控制
|
网络安全
编译原理复习二:Top-Down分析LL(1)文法的判断与LL(1)分析表的构造(附题目与答案 超详细)
编译原理复习二:Top-Down分析LL(1)文法的判断与LL(1)分析表的构造(附题目与答案 超详细)
987 1
|
Ubuntu
Ubuntu 18.04中卸载及安装G2O库
Ubuntu 18.04中卸载及安装G2O库
1030 0
|
6月前
|
数据采集 人工智能 边缘计算
爬虫IP代理效率优化:策略解析与实战案例
本文深入探讨了分布式爬虫中代理池效率优化的关键问题。首先分析了代理效率瓶颈的根源,包括不同类型代理的特点、连接耗时及IP失效问题。接着提出了六大核心优化策略:智能IP轮换矩阵、连接复用优化、动态指纹伪装、智能重试机制等,并结合电商价格监控、社交媒体舆情分析和金融数据抓取三个实战案例,展示了优化效果。同时建立了三维效率评估体系,从质量、成本和稳定性全面衡量性能。最后展望了AI驱动调度、边缘计算融合等未来演进方向,帮助爬虫系统实现从“暴力采集”到“智能获取”的进化,大幅提升效率并降低成本。
232 0
|
存储 Java
Java 中 ArrayList 的初始大小是多少?
【8月更文挑战第23天】
481 0
【IntelliJ IDEA】idea 收起注释、打开注释、隐藏注释的快捷键
【IntelliJ IDEA】idea 收起注释、打开注释、隐藏注释的快捷键
1227 0
|
机器学习/深度学习 人工智能 Dart
AI - 机器学习GBDT算法
梯度提升决策树(Gradient Boosting Decision Tree),是一种集成学习的算法,它通过构建多个决策树来逐步修正之前模型的错误,从而提升模型整体的预测性能。
|
小程序 JavaScript Java
二手交易|校园二手交易小程序|基于微信小程序的闲置物品交易平台设计与实现(源码+数据库+文档)
二手交易|校园二手交易小程序|基于微信小程序的闲置物品交易平台设计与实现(源码+数据库+文档)
1010 2
|
Docker 容器
GitLab Runner注册大揭秘:高效CI/CD的入门指南
GitLab Runner注册大揭秘:高效CI/CD的入门指南
531 0
GitLab Runner注册大揭秘:高效CI/CD的入门指南
|
机器学习/深度学习 算法 数据安全/隐私保护
图像滤镜艺术---人脸编辑(五官微调+瘦脸美型)
原文:图像滤镜艺术---人脸编辑(五官微调+瘦脸美型) 写本文的目的,实际上是对目前人脸美型这一块技术做个总结,跟大家 分享一下!目前提到美颜算法,大家都会想到磨皮美白 /大眼瘦脸,实际上做好 美颜这件事情,关乎的不仅仅是这些,还有五官的协调比例等,今天我们主要说一下五官的微调,这里我直接称之为人脸编辑吧。
3782 0