当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- A certification and authorization solution based on. Net core - hulutong 1.0 open source
- 5分钟GET我使用Github 5 年总结的这些骚操作!
- Data consistency of cache
- 深入分析商淘多用户商城系统如何从搜索着手打造盈利点
- Echart sets the spacing between columns
- AutoCAD2020 完整版安装图文教程、注册激活破解方法
- 超大折扣力度,云服务器 88 元秒杀
- Do programmers pay too much to work overtime? Should programmer's salary be reduced? Netizen: let go of other hard pressed programmers
- Implement printf function by yourself
- 拉线式位移传感器在边坡裂缝中的作用
猜你喜欢

高德全链路压测——语料智能化演进之路

博士在读时,把暗恋的师兄变成了老公是种怎样的体验?

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

同事笔记-小程序入坑点

Openyurt in depth interpretation: how to build kubernetes native cloud edge efficient collaborative network?

Talking about PHP file fragment upload from a requirement improvement

AutoCAD 2020 installation package & Installation Tutorial

【运维思考】如何做好云上运维服务?

使用Fastai开发和部署图像分类器应用

CAD2016下载AutoCAD2016下载安装详细教程CAD下载
随机推荐
Full stack technology experience tells you: how much does it cost to develop a mall small program?
听说你一夜之间变了户籍,依萍如洗的打工人该如何自救?
融云完成数亿人民币 D 轮融资,将持续打造全球云通信能力
Arthas install quick installation document
Source code analysis of serilog -- implementation of sink
5 minutes get I use GitHub's 5-year summary of these operations!
岗位内推 | 微软亚洲研究院智能多媒体组招聘计算机视觉算法实习生
arthas无网络环境下离线安装方法
Embedded assembly in IOS
A certification and authorization solution based on. Net core - hulutong 1.0 open source
Do programmers pay too much to work overtime? Should programmer's salary be reduced? Netizen: let go of other hard pressed programmers
寻找性能更优秀的不可变小字典
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王健
The technology masters who ride the wind and waves gather again | Tencent cloud TVP continues to sail
毕业一年后接私活赚了10w,还拿了几家大厂offer!
5 minutes get I use GitHub's five-year summary of these complaints!
shell脚本快速入门----shell基本语法总结
MES system is different from traditional management in industry application
AE(After Effects)的简单使用——记一次模板套用的过程
Restart the heap_ uaf_ hacknote