当前位置:网站首页>浮点数之间的等值判断
浮点数之间的等值判断
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
边栏推荐
- spark学习(二)--作业调度和shuffle解析
- CCF BDCI hot topic: privacy information recognition in unstructured business text information
- Full link stress testing of moral integrity -- the evolution of corpus intelligence
- Spark Learning (2) -- job scheduling and shuffle analysis
- How to use Camtasia to make dynamic animation scene?
- 详解三种不同的身份验证协议
- Programmers before and after buying a house, after reading has cried blind
- Low power Bluetooth single chip helps Internet of things
- A certification and authorization solution based on. Net core - hulutong 1.0 open source
- Booker · apachecn programming / back end / big data / AI learning resources 2020.11
猜你喜欢
什么是网站【新四化】?
从一次需求改良漫谈php文件分片上传
Talking about PHP file fragment upload from a requirement improvement
iOS中的内嵌汇编
5 minutes get I use GitHub's five-year summary of these complaints!
MES系统在工厂生产管理起到9大很重要的作用
SEO建设者,有哪些说不出的苦?
博士在读时,把暗恋的师兄变成了老公是种怎样的体验?
腾讯云服务器搭建wordpress网站的两种方式(详细图文新手版)
Android byte beat one side, was hanged by the interviewer! Fortunately, we got the offer from three sides
随机推荐
史上最惨黑客:偷走10亿美元比特币7年未花,最终被司法部全数缴获
从一次需求改良漫谈php文件分片上传
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
H5公众号点击内置浏览器的关闭(左上角的叉叉)监听到事件
同事笔记-小程序入坑点
spark学习(三)--内存管理和性能调优
移动安全加固助力 App 实现全面、有效的安全防护
Put method of immutablemap
使用art-Template模板获取天气预报信息
国际顶刊Radiology发表华为云最新联合成果,AI辅助检测脑动脉瘤
Decision tree algorithm theory
Full link stress testing of moral integrity -- the evolution of corpus intelligence
程序员过高工资导致加班?应该降低程序员工资?网友:放过其他苦逼的程序员吧
你的钱为什么会被转走,这篇文章告诉你答案
腾讯云服务器搭建wordpress网站的两种方式(详细图文新手版)
Talking about PHP file fragment upload from a requirement improvement
SEO建设者,有哪些说不出的苦?
Chinese programmer vs Japanese programmer, full screen shame!
Using art template to obtain weather forecast information
【亲测有效】Github无法访问或者访问速度的解决方案