根据这个java.sun页面 ==是Java中浮点数的相等比较运算符.
但是,当我输入以下代码时:
if(sectionID == currentSectionID)
Run Code Online (Sandbox Code Playgroud)
进入我的编辑器并运行静态分析,我得到:"JAVA0078浮点值与==相比"
使用==比较浮点值有什么问题?这样做的正确方法是什么?
我写了一个类来测试相等,小于和大于Java中的两个双重.我的一般情况是比较可以具有半分精度的价格.59.005比59.395.我选择的epsilon适合这些情况吗?
private final static double EPSILON = 0.00001;
/**
* Returns true if two doubles are considered equal. Tests if the absolute
* difference between two doubles has a difference less then .00001. This
* should be fine when comparing prices, because prices have a precision of
* .001.
*
* @param a double to compare.
* @param b double to compare.
* @return true true if two doubles are considered equal.
*/
public static boolean equals(double a, double b){ …Run Code Online (Sandbox Code Playgroud)