当前位置:网站首页>Accuracy of BigDecimal Division
Accuracy of BigDecimal Division
2022-07-05 17:34:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
BigDecimal The accuracy of division
In the use of BigDecimal Division time , Encounter a ghost animal problem , The accuracy of this calculation , Result use return 0, Of course, I finally found that my posture was wrong , So record , Avoid repeating the mistakes later
I. Ask questions
In the use of BigDecimal When doing high-precision Division , I encountered a small problem when I didn't pay attention , as follows
@Test
public void testBigDecimal() {
BigDecimal origin = new BigDecimal(541253);
BigDecimal now = new BigDecimal(12389431);
BigDecimal val = origin.divide(now, RoundingMode.HALF_UP);
System.out.println(val);
origin = new BigDecimal(541253);
now = new BigDecimal(12389431.3);
val = origin.divide(now, RoundingMode.HALF_UP);
System.out.println(val);
origin = new BigDecimal(541253.4);
now = new BigDecimal(12389431);
val = origin.divide(now, RoundingMode.HALF_UP);
System.out.println(val);
}
Copy code What is the output above ?
0
0
0.043686703610520937021487456961257
Copy code Why are the first two 0 Well , If it's directly 541253 / 12389431 = 0 It's understandable , however BigDecimal Isn't it a high-precision calculation , To be reasonable, there should not be such a problem of division
We know that BigDecimal When triggering , You can specify parameters to keep decimals , If you add this , Will it be different ?
BigDecimal origin = new BigDecimal(541253);
BigDecimal now = new BigDecimal(12389431);
BigDecimal val = origin.divide(now, 5, RoundingMode.HALF_UP);
System.out.println(val);
Copy code The output is :
0.04369
Copy code So after the decimal point is specified , No problem , So make a bold guess , Is it some of the above case in , because scale When the value is not specified , The default values are different , Which leads to different accuracy of the final result ?
Simple in-depth source code analysis , The way of execution is origin.divide(now, RoundingMode.HALF_UP);, So this scale Parameters are aimed origin object , And this object , We can only analyze its structure , Because there is no other place to use
II. Source location
1. Shaping parameter transmission structure
Analyze the following line , Go straight to source
BigDecimal origin = new BigDecimal(541253);
Copy code The obvious int Pass parameter structure , Go in and have a brief look
// java.math.BigDecimal#BigDecimal(int)
public BigDecimal(int val) {
this.intCompact = val;
this.scale = 0;
this.intVal = null;
}
public BigDecimal(long val) {
this.intCompact = val;
this.intVal = (val == INFLATED) ? INFLATED_BIGINT : null;
this.scale = 0;
}
Copy code so, Clearly know the default scale by 0, That is to say, when origin Positive number , Division with it , Unrealistic designation scale When parameters are , The final returns are all without decimals , Take the same look , also long The way of transmitting reference , BigInteger It's the same thing
2. Floating point parameter transfer
The next step is floating point scale The default value confirms , This structure is a little more complex than the previous one , Source code is not posted , Too long , I don't quite understand what I did , Directly in a more obscene way , Get into debug Pattern , Step by step
@Test
public void testBigDecimal() {
BigDecimal origin = new BigDecimal(541253.0);
BigDecimal now = new BigDecimal(12389431.1);
BigDecimal tmp = new BigDecimal(0.0);
}
Copy code according to debug Result , first ,scale by 0; the second scale by 29, Third scale by 0
3. String The ginseng
It's still a bunch of logic , Also use one-step debug Try... In a different way
@Test
public void testBigDecimal() {
BigDecimal origin = new BigDecimal("541253.0");
BigDecimal now = new BigDecimal("12389431.1");
BigDecimal t = new BigDecimal("0.0");
}
Copy code The top three scale All are 1
4. Summary
- about BigDecimal In Division , It is best to specify its scale Parameters , Or there might be a hole
- about BigDecimla Of scale The principle of initialization , Need to take a closer look BigDecimal How did it happen
II. other
1. A gray Blog: https://liuyueyi.github.io/hexblog
A grey personal blog , Keep a record of all the blogs in your study and work , Welcome to visit
2. Statement
Better to believe than to believe , What has been said , It's all in one family , Because of limited personal ability , There are inevitably omissions and mistakes , If found bug Or better advice , Welcome criticism and correction , Thank you very much
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/149837.html Link to the original text :https://javaforall.cn
边栏推荐
- Tita 绩效宝:如何为年中考核做准备?
- 漫画:如何实现大整数相乘?(下)
- ternary operator
- URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
- Error in compiling libssh2. OpenSSL cannot be found
- flask解决CORS ERR 问题
- About JSON parsing function JSON in MySQL_ EXTRACT
- 网络威胁分析师应该具备的十种能力
- 服务器配置 jupyter环境
- Zhang Ping'an: accélérer l'innovation numérique dans le cloud et construire conjointement un écosystème industriel intelligent
猜你喜欢

服务器配置 jupyter环境

哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?

Design of electronic clock based on 51 single chip microcomputer

統計php程序運行時間及設置PHP最長運行時間

ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲

Knowledge points of MySQL (6)

北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员

VBA驱动SAP GUI实现办公自动化(二):判断元素是否存在

Short the command line via jar manifest or via a classpath file and rerun

ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée
随机推荐
域名解析,反向域名解析nbtstat
Webapp development - Google official tutorial
漫画:一道数学题引发的血案
机器学习01:绪论
网络威胁分析师应该具备的十种能力
Read the basic grammar of C language in one article
How to write a full score project document | acquisition technology
To solve the problem of "double click PDF file, pop up", please install Evernote program
Tita performance treasure: how to prepare for the mid year examination?
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
Machine learning 01: Introduction
Check the WiFi password connected to your computer
Rider 设置选中单词侧边高亮,去除警告建议高亮
flask解决CORS ERR 问题
Matery主题自定义(一)黑夜模式
Error in compiling libssh2. OpenSSL cannot be found
MySQL queries the latest qualified data rows
漫画:有趣的海盗问题 (完整版)
如何保存训练好的神经网络模型(pytorch版本)
云主机oracle异常恢复----惜分飞