Java双重比较 [英] Java Double Comparison
本文介绍了Java双重比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
eg
public static boolean greaterThanOrEqual(double a,double b,double epsilon){
return a - b> -epsilon;
}
每个项目我开始我最终重新实现这个和复制粘贴代码并且测试。
注意一个很好的例子,为什么更好地使用第三方JAR是IBM推荐以下内容:
如果您不了解基础测量的规模,则使用
测试abs(a / b - 1)< epsilon可能比简单的
比较差异更强大
我怀疑很多人会想到这个并且说明即使是简单的代码可能是次优的。
解决方案
Guava 有 DoubleMath.fuzzyCompare()
。
Are there any java libraries for doing double comparison? e.g.
public static boolean greaterThanOrEqual(double a, double b, double epsilon){
return a - b > -epsilon;
}
Every project I start I end up re-implementing this and copy-pasting code and test.
NB a good example of why its better to use 3rd party JARs is that IBM recommend the following:
"If you don't know the scale of the underlying measurements, using the test "abs(a/b - 1) < epsilon" is likely to be more robust than simply comparing the difference"
I doubt many people would have thought of this and illustrates that even simple code can be sub-optimal.
解决方案
Guava has DoubleMath.fuzzyCompare()
.
这篇关于Java双重比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文