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

简介: 记录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) 返回该 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) 返回的值是 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) 翻译 double成 BigDecimal,使用 double正则字符串表示的 Double.toString(double)方法提供。
static BigDecimal valueOf(long val) 翻译 long值变成零规模 BigDecimal。
static BigDecimal valueOf(long unscaledVal, int scale) 翻译 long不成比例的价值和 int规模为 BigDecimal。
相关文章
|
安全 架构师
【企业架构】什么是 TOGAF? 企业架构方法论
【企业架构】什么是 TOGAF? 企业架构方法论
|
存储 Java Android开发
Rockchip系列之UART 新增framework系统jni+service接口访问(2)
Rockchip系列之UART 新增framework系统jni+service接口访问(2)
369 1
|
安全 Oracle 网络协议
第4章 数据库安全性——4.1 数据库安全性概述
第4章 数据库安全性——4.1 数据库安全性概述
|
3月前
|
JSON 编解码 API
百炼视频能力栈实测:文生/图生/首尾帧三条链路的参数矩阵与计价口径
百炼视频能力分happyhorse-1.1(t2v/i2v/r2v)与wan3.0-video两大模型家族,支持文生/图生/首尾帧等五类生成及参考视频、编辑等功能。命令统一入口为`bl video`,六次实跑全部成功,清晰度与水印可调,免费额度限华北2地域,需注意seed不保证复现。
|
消息中间件 缓存 前端开发
WebSocket 与 MQTT 在即时通讯中的深度对比与架构选型指南
WebSocket 是双向通信通道,适合前端实时交互;MQTT 是轻量级消息协议,支持发布/订阅与可靠传输。二者互补,常结合使用:前端通过 WebSocket 接入,后端以 MQTT 实现高并发消息分发,构建可扩展的现代即时通讯系统。
2181 17
|
数据采集 数据可视化 数据挖掘
从信息捕获到多维研判的链路解析
本案例构建了一套基于爬虫与数据分析的热点监测系统,通过代理IP与Python工具实现新闻内容抓取,结合时间、来源与关键词分析,打造“信息雷达”,助力舆情研判与趋势预测。
491 7
从信息捕获到多维研判的链路解析
|
存储 安全 数据安全/隐私保护
ERP系统的灾备与数据恢复:保障企业业务连续性
【7月更文挑战第29天】 ERP系统的灾备与数据恢复:保障企业业务连续性
886 2
|
机器学习/深度学习 人工智能 分布式计算
【AI系统】混合并行
混合并行融合了数据并行、模型并行和流水线并行,旨在高效利用计算资源,尤其适合大规模深度学习模型训练。通过将模型和数据合理分配至多个设备,混合并行不仅提升了计算效率,还优化了内存使用,使得在有限的硬件条件下也能处理超大型模型。3D混合并行(DP+PP+TP)是最先进的形式,需至少8个GPU实现。此策略通过拓扑感知3D映射最大化计算效率,减少通信开销,是当前深度学习训练框架如Deepspeed和Colossal AI的核心技术之一。
1104 15
【AI系统】混合并行
|
存储 Java 程序员
Python基本概念
Python基本概念
414 0
|
安全 网络安全 数据库
localhost知识
localhost知识
7807 0