epsilon java,Java双比较epsilon

博客指出使用浮点数(如double)表示货币是不专业的,因为浮点数无法精确表示小数,可能导致错误。推荐使用`java.math.BigDecimal`来处理货币,它可以提供指定精度的运算,避免精度丢失。此外,还讨论了比较浮点数时的误差问题,并提供了使用epsilon进行比较的方法。
摘要由CSDN通过智能技术生成

I wrote a class that tests for equality, less than, and greater than with two doubles in Java. My general case is comparing price that can have an accuracy of a half cent. 59.005 compared to 59.395. Is the epsilon I chose adequate for those cases?

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;

}

解决方案

You do NOT use double to represent money. Not ever. Use java.math.BigDecimal instead.

Then you can specify how exactly to do rounding (which is sometimes dictated by law in financial applications!) and don't have to do stupid hacks like this epsilon thing.

Seriously, using floating point types to represent money is extremely unprofessional.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值