当前位置:网站首页>浮点数之间的等值判断
浮点数之间的等值判断
2020-11-09 16:06:00 【龘游戏人生龘】
(1) 指定一个误差范围,两个浮点数的差值在此范围之内,则认为是相等的。
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) 使用 BigDecimal 来定义值,再进行浮点数的运算操作。
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");
}
如上所示 BigDecimal 的等值比较应使用 compareTo()方法,而不是 equals()方法。
说明:equals()方法会比较值和精度(1.0 与 1.00 返回结果为 false),而 compareTo()则会忽略精度。
出自:Java开发手册(嵩山版)
版权声明
本文为[龘游戏人生龘]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/3628379/blog/4710132
边栏推荐
- 程序员买房前后对比,看完后已哭瞎...
- Offline installation method of Arthas without network environment
- 5 minutes get I use GitHub's five-year summary of these complaints!
- Full link stress testing of moral integrity -- the evolution of corpus intelligence
- Why does it take more and more time to develop a software?
- Full stack technology experience tells you: how much does it cost to develop a mall small program?
- Super discount, cloud server 88 yuan seconds
- In depth analysis of the multi-user shopping mall system from search to create a profit point
- H5公众号点击内置浏览器的关闭(左上角的叉叉)监听到事件
- Autocad2020 full version installation text course, registration activation cracking method
猜你喜欢

Colleague notes - small program entry point

用微信表情翻译表白,程序员的小浪漫,赶紧Get起来!

Application of EMQ X in the Internet of things platform of China Construction Bank

Guest interview: Wang Jian

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

MES系统在行业应用里区别于传统式管理

微服务框架 Go-Micro 集成 Nacos 实战之服务注册与发现

5 minutes get I use GitHub's 5-year summary of these operations!

Using fastai to develop and deploy image classifier application

. net report builder stimulsoft Reports.Net Release the latest version of v2020.5!
随机推荐
Object oriented magic method collection
MES系统在行业应用里区别于传统式管理
Echart sets the spacing between columns
To me, multithreading transaction must be a pseudo proposition!
决策树算法-理论篇
Method of conversion between JS character and ASCII code
Cad2016 download autocad2016 download installation detailed tutorial CAD Download
MES system is different from traditional management in industry application
Full link stress testing of moral integrity -- the evolution of corpus intelligence
Using fastai to develop and deploy image classifier application
Chinese programmer vs Japanese programmer, full screen shame!
In depth analysis of the multi-user shopping mall system from search to create a profit point
CAD tutorial cad2016 installation course
iOS中的内嵌汇编
On agile development concept and iterative development scheme
最新版PyCharm 2020.3 :可实现结对编程,智能文本校对等|附下载体验
MES系统在工厂生产管理起到9大很重要的作用
MES系统在行业应用里区别于传统式管理
Installation and testing of Flink
Android byte beat one side, was hanged by the interviewer! Fortunately, we got the offer from three sides