当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 从一次需求改良漫谈php文件分片上传
- A quick start to Shell Scripting
- Express yourself with wechat expression translation, programmer's little romance, get up quickly!
- 高德全链路压测——语料智能化演进之路
- Position promotion | intelligent multimedia group of Microsoft Asia research institute recruits computer vision algorithm Intern
- Source code analysis of serilog -- implementation of sink
- 5分钟GET我使用Github 5 年总结的这些骚操作!
- CAD tutorial cad2016 installation course
- Offline installation method of Arthas without network environment
- 在Python中创建文字云或标签云
猜你喜欢

CAD2020下载AutoCAD2020下载安装教程AutoCAD2020中文下载安装方法

Arthas Install 快速安装文档

拉线位移编码器的选择,需要精量电子的指导

Rongyun has completed several hundred million RMB round D financing, and will continue to build global cloud communication capability

Full link stress testing of moral integrity -- the evolution of corpus intelligence

你以为学编程很简单吗,其实它很难!你以为学编程很难吗,其实它很简单!

移动安全加固助力 App 实现全面、有效的安全防护

High quality defect analysis: let yourself write fewer bugs

从一次需求改良漫谈php文件分片上传

One year after graduation, I took private jobs to earn 10 W and got offers from several big factories!
随机推荐
高质量的缺陷分析:让自己少写 bug
毕业一年后接私活赚了10w,还拿了几家大厂offer!
I heard that you changed your registered residence overnight. How can you help yourself if you work like ping?
Explore cache configuration of Android gradle plug-in
I do digital transformation in traditional industries (1)
SEO建设者,有哪些说不出的苦?
一款基于.NET Core的认证授权解决方案-葫芦藤1.0开源啦
Arthas Install 快速安装文档
Cad2016 software installation tutorial
Data consistency of cache
程序员过高工资导致加班?应该降低程序员工资?网友:放过其他苦逼的程序员吧
Express yourself with wechat expression translation, programmer's little romance, get up quickly!
Source code analysis of serilog -- implementation of sink
如何使用Camtasia制作动态动画场景?
MES系统在行业应用里区别于传统式管理
MES system plays an important role in the factory production management
【运维思考】如何做好云上运维服务?
arthas无网络环境下离线安装方法
js字符与ASCII码互转的方法
Using GaN based oversampling technique to improve the accuracy of model for mortality prediction of unbalanced covid-19