当前位置:网站首页>浮点数之间的等值判断
浮点数之间的等值判断
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
边栏推荐
- 决策树算法-理论篇
- To me, multithreading transaction must be a pseudo proposition!
- Offline installation method of Arthas without network environment
- International top journal radiology published the latest joint results of Huawei cloud, AI assisted detection of cerebral aneurysms
- CCF BDCI热门赛题:非结构化商业文本信息中隐私信息识别
- Application of EMQ X in the Internet of things platform of China Construction Bank
- Why does it take more and more time to develop a software?
- MES system plays an important role in the factory production management
- Using fastai to develop and deploy image classifier application
- 腾讯云服务器搭建wordpress网站的两种方式(详细图文新手版)
猜你喜欢

MES system plays an important role in the factory production management

听说你一夜之间变了户籍,依萍如洗的打工人该如何自救?

MES system is different from traditional management in industry application

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

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

深入探索 Android Gradle 插件的缓存配置

Simple use of AE (after effects)

SEO builders, what are the unspeakable hardships?

Talking about PHP file fragment upload from a requirement improvement

Chinese programmer vs Japanese programmer, full screen shame!
随机推荐
Two ways for Tencent cloud server to build WordPress website
CAD2020下载AutoCAD2020下载安装教程AutoCAD2020中文下载安装方法
乘风破浪的技术大咖再次集结 | 腾讯云TVP持续航行中
使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
Decision tree algorithm theory
Spark Learning (2) -- job scheduling and shuffle analysis
To me, multithreading transaction must be a pseudo proposition!
International top journal radiology published the latest joint results of Huawei cloud, AI assisted detection of cerebral aneurysms
堆重启_uaf_hacknote
CAD tutorial cad2016 installation course
用会声会影替换视频背景原来这么简单
Mobile security reinforcement helps app achieve comprehensive and effective security protection
What kind of experience does a doctor have when he turns his secret love brother into a husband?
I heard that you changed your registered residence overnight. How can you help yourself if you work like ping?
最新版PyCharm 2020.3 :可实现结对编程,智能文本校对等|附下载体验
spark学习(三)--内存管理和性能调优
Offline installation method of Arthas without network environment
AutoCAD 2020 installation package & Installation Tutorial
OpenYurt 深度解读:如何构建 Kubernetes 原生云边高效协同网络?
AutoCAD2020 完整版安装图文教程、注册激活破解方法