久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java 雙重比較 epsilon

Java double comparison epsilon(Java 雙重比較 epsilon)
本文介紹了Java 雙重比較 epsilon的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我編寫了一個類,用 Java 中的兩個雙精度來測試相等、小于和大于.我的一般情況是比較可以精確到半美分的價格.59.005 與 59.395 相比.我為這些情況選擇的 epsilon 是否足夠?

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;
}

推薦答案

不要用 double 來代表錢.永遠不會.使用 java.math.BigDecimal 代替.

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

然后,您可以指定如何精確地進行舍入(有時在金融應用程序中由法律規定!),而不必像 epsilon 那樣做愚蠢的黑客行為.

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.

這篇關于Java 雙重比較 epsilon的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 亚洲色图50p| 欧美一区二区三区电影 | 亚洲视频免费一区 | 中文字幕国产精品 | 精品亚洲一区二区三区 | 国产精产国品一二三产区视频 | 成人九色 | 国产高清在线观看 | 亚洲精品一区二区三区蜜桃久 | 成人九色 | 精品国产一区二区三区日日嗨 | 日韩一区二区在线视频 | 欧美黑人国产人伦爽爽爽 | 国产亚洲精品综合一区 | 亚洲综合婷婷 | 99re在线视频观看 | 欧美一级片久久 | 涩爱av一区二区三区 | 免费在线成人 | 精品一区二区三区免费毛片 | 亚洲欧美视频 | 成人永久免费 | 插插插干干干 | 免费精品视频 | 精品久久久久久久久久久下田 | 国产日韩欧美二区 | 日韩欧美中文 | 久久久激情视频 | 久久成人一区二区三区 | 91精品国产乱码麻豆白嫩 | 亚洲一区二区av在线 | 亚洲精品久久久一区二区三区 | 亚洲成人国产综合 | 精彩视频一区二区三区 | 在线视频亚洲 | 午夜精品视频一区 | 亚洲一在线 | av中文字幕在线观看 | 久久久精| 91精品国产色综合久久 | 亚洲精品国产一区 |