当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- CCF BDCI hot topic: privacy information recognition in unstructured business text information
- 为什么现在开发一款软件的时间越来越长?
- 低功耗蓝牙单芯片为物联网助力
- 用微信表情翻译表白,程序员的小浪漫,赶紧Get起来!
- 树莓派内网穿透建站与维护,使用内网穿透无需服务器
- Implement printf function by yourself
- 大厂面试系列(二):并发编程
- Express yourself with wechat expression translation, programmer's little romance, get up quickly!
- HomeLede 2020.11.08 v5.4.72内核 UPnP+NAS+多拨+网盘+DNS优化+帕斯沃 无缝集成+软件包
- 一款基于.NET Core的认证授权解决方案-葫芦藤1.0开源啦
猜你喜欢
EMQ X 在中国建设银行物联网平台中的应用EMQ X 在中国建设银行物联网平台中的应用
SEO建设者,有哪些说不出的苦?
Data consistency of cache
瞧瞧,这样的『函数』才叫 Pythonic
CCF BDCI热门赛题:非结构化商业文本信息中隐私信息识别
函数计算进阶-IP查询工具开发
拉线式位移传感器在边坡裂缝中的作用
A certification and authorization solution based on. Net core - hulutong 1.0 open source
The way of a million year salary Architect: on the architecture design of application system
Implement printf function by yourself
随机推荐
干货推荐:关于网络安全技术的专业术语,你知道多少?
A certification and authorization solution based on. Net core - hulutong 1.0 open source
How can you be a big data worker with an annual salary of 40W if you don't work hard?
Guest interview: Wang Jian
CAD tutorial cad2016 installation course
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王健
高质量的缺陷分析:让自己少写 bug
CAD2016软件安装教程
Full stack technology experience tells you: how much does it cost to develop a mall small program?
Installation and testing of Flink
详解三种不同的身份验证协议
Performance comparison of serialization tools such as Jackson, fastjson, kryo, protostuff
Super discount, cloud server 88 yuan seconds
堆重启_uaf_hacknote
shell脚本快速入门----shell基本语法总结
CCF BDCI热门赛题:非结构化商业文本信息中隐私信息识别
使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
cad教程 cad2016安装教程
我叫Mongo,收了「查询基础篇」,值得你拥有
史上最惨黑客:偷走10亿美元比特币7年未花,最终被司法部全数缴获