首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java双重比较epsilon

Java双重比较epsilon
EN

Stack Overflow用户
提问于 2008-12-10 17:11:15
回答 9查看 108.7K关注 0票数 53

我用Java编写了一个类,用来测试相等、小于和大于两个双精度值。我的一般情况是比较价格,可以有0.5美分的准确性。59.005,而不是59.395。我选择的epsilon是否适合这些情况?

代码语言:javascript
复制
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){
    return a == b ? true : Math.abs(a - b) < EPSILON;
}


/**
 * Returns true if two doubles are considered equal. Tests if the absolute
 * difference between the two doubles has a difference less then a given
 * double (epsilon). Determining the given epsilon is highly dependant on the
 * precision of the doubles that are being compared.
 *
 * @param a double to compare.
 * @param b double to compare
 * @param epsilon double which is compared to the absolute difference of two
 * doubles to determine if they are equal.
 * @return true if a is considered equal to b.
 */
public static boolean equals(double a, double b, double epsilon){
    return a == b ? true : Math.abs(a - b) < epsilon;
}


/**
 * Returns true if the first double is considered greater than the second
 * double.  Test if the difference of first minus second is greater then
 * .00001.  This should be fine when comparing prices, because prices have a
 * precision of .001.
 *
 * @param a first double
 * @param b second double
 * @return true if the first double is considered greater than the second
 *              double
 */
public static boolean greaterThan(double a, double b){
    return greaterThan(a, b, EPSILON);
}


/**
 * Returns true if the first double is considered greater than the second
 * double.  Test if the difference of first minus second is greater then
 * a given double (epsilon).  Determining the given epsilon is highly
 * dependant on the precision of the doubles that are being compared.
 *
 * @param a first double
 * @param b second double
 * @return true if the first double is considered greater than the second
 *              double
 */
public static boolean greaterThan(double a, double b, double epsilon){
    return a - b > epsilon;
}


/**
 * Returns true if the first double is considered less than the second
 * double.  Test if the difference of second minus first is greater then
 * .00001.  This should be fine when comparing prices, because prices have a
 * precision of .001.
 *
 * @param a first double
 * @param b second double
 * @return true if the first double is considered less than the second
 *              double
 */
public static boolean lessThan(double a, double b){
    return lessThan(a, b, EPSILON);
}


/**
 * Returns true if the first double is considered less than the second
 * double.  Test if the difference of second minus first is greater then
 * a given double (epsilon).  Determining the given epsilon is highly
 * dependant on the precision of the doubles that are being compared.
 *
 * @param a first double
 * @param b second double
 * @return true if the first double is considered less than the second
 *              double
 */
public static boolean lessThan(double a, double b, double epsilon){
    return b - a > epsilon;
}
EN

回答 9

Stack Overflow用户

发布于 2008-12-10 17:24:11

您不能使用double来表示货币。永远不会。请改用java.math.BigDecimal

然后,您可以指定如何精确地进行舍入(在金融应用程序中,这有时是由法律规定的!)也不需要像epsilon那样做愚蠢的黑客操作。

严肃地说,使用浮点类型来表示货币是非常不专业的。

票数 112
EN

Stack Overflow用户

发布于 2008-12-10 17:15:13

是。Java doubles将保持比给定的epsilon 0.00001更好的精度。

由于存储浮点值而发生的任何舍入误差都将小于0.00001。在Java语言中,我经常使用1E-6或0.000001作为双epsilon,没有任何问题。

另外,我喜欢epsilon = 1E-5;的格式,因为我觉得它更具可读性( Java中的1E-5=1x10^-5)。在阅读代码时,1E-6和1E-5很容易区分,而当浏览代码时,0.00001和0.000001看起来非常相似,我认为它们是相同的值。

票数 16
EN

Stack Overflow用户

发布于 2008-12-10 17:21:14

哇,哇,哇。有没有什么特别的原因让你使用浮点数作为货币,或者使用arbitrary-precision, fixed-point number format会更好?我不知道你试图解决的具体问题是什么,但你应该考虑一下,半美分是否真的是你想要处理的东西,或者它只是使用不精确的数字格式的产物。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/356807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档