当前位置:网站首页>Equivalent judgment between floating point numbers
Equivalent judgment between floating point numbers
2020-11-09 16:06:00 【Game life】
(1) Specify an error range , The difference between two floating-point numbers is in this range , It is considered equal .
float a = 1.0F - 0.9F;
float b = 0.9F - 0.8F;
float diff = 1e-6F;
if (Math.abs(a - b) < diff) {
System.out.println("true");
}
(2) Use BigDecimal To define the value , And then the floating-point operation .
BigDecimal a = new BigDecimal("1.0");
BigDecimal b = new BigDecimal("0.9");
BigDecimal c = new BigDecimal("0.8");
BigDecimal x = a.subtract(b);
BigDecimal y = b.subtract(c);
if (x.compareTo(y) == 0) {
System.out.println("true");
}
As shown above BigDecimal The equivalent comparison of should use compareTo() Method , instead of equals() Method .
explain :equals() Methods compare values and precision (1.0 And 1.00 The return result is false), and compareTo() Precision is ignored .
come from :Java Development Manual ( Song Mountain version )
版权声明
本文为[Game life]所创,转载请带上原文链接,感谢
边栏推荐
- AutoCAD 2020 installation package & Installation Tutorial
- A certification and authorization solution based on. Net core - hulutong 1.0 open source
- 决策树算法-理论篇
- Echart sets the spacing between columns
- CAD tutorial cad2016 installation course
- One year after graduation, I took private jobs to earn 10 W and got offers from several big factories!
- International top journal radiology published the latest joint results of Huawei cloud, AI assisted detection of cerebral aneurysms
- python中常见的一些错误异常类型
- Autocad2020 full version installation text course, registration activation cracking method
- Flink的安装和测试
猜你喜欢
Autocad2020 full version installation text course, registration activation cracking method
SEO builders, what are the unspeakable hardships?
设置背景图片的两种方式,并解决手机端背景图片高度自适应问题
MES system is different from traditional management in industry application
程序员过高工资导致加班?应该降低程序员工资?网友:放过其他苦逼的程序员吧
Simple use of AE (after effects)
What is website [new four modernizations]?
iOS中的内嵌汇编
Object oriented magic method collection
移动安全加固助力 App 实现全面、有效的安全防护
随机推荐
SEO builders, what are the unspeakable hardships?
I do digital transformation in traditional industries (1)
同事笔记-小程序入坑点
使用Fastai开发和部署图像分类器应用
Explore cache configuration of Android gradle plug-in
【运维思考】如何做好云上运维服务?
树莓派内网穿透建站与维护,使用内网穿透无需服务器
js字符与ASCII码互转的方法
The latest version of pycharm 2020.3: pair programming, intelligent text proofreading and downloading experience
Using GaN based oversampling technique to improve the accuracy of model for mortality prediction of unbalanced covid-19
OpenYurt 深度解读:如何构建 Kubernetes 原生云边高效协同网络?
Chinese programmer vs Japanese programmer, full screen shame!
Arthas Install 快速安装文档
Restart the heap_ uaf_ hacknote
The worst hacker in history: stealing $1 billion of bitcoin without spending it for seven years, and finally being seized by the Department of justice
High quality defect analysis: let yourself write fewer bugs
Native地图与Web融合技术的应用与实践
Service registration and discovery of go micro integration Nacos
华为云GaussDB:从颠覆自我到颠覆行业,重构数据库市场新格局
使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性