久久久久久久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(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产96色在线 | 欧美一级网站 | 中文字幕一区二区三区四区不卡 | 福利视频网站 | 久久国产日本 | 毛片网站在线观看 | 日韩欧美一区二区三区 | 亚洲精品一区二区三区蜜桃久 | 亚洲小视频在线播放 | 97人人澡人人爽91综合色 | 亚洲精品视频一区二区三区 | 最新日韩av| 久久久久亚洲精品 | 夜夜骑综合 | 国产精品久久久久久一区二区三区 | 国产一区二区免费在线 | 福利视频亚洲 | 在线观看www | 国产免费看 | 久久婷婷国产麻豆91 | 日韩在线国产 | 91高清在线视频 | 精品久久影院 | 91免费在线视频 | 亚洲大片在线观看 | 亚洲欧美在线一区 | 国产视频线观看永久免费 | 国产精品精品视频一区二区三区 | 日韩电影一区 | 欧产日产国产精品视频 | 成人午夜电影在线观看 | 中日字幕大片在线播放 | 亚洲精品视频在线看 | 国产精品久久av | 欧美区在线观看 | 中文字幕1区2区 | 国产精品日韩欧美一区二区 | 日韩一区二区三区在线 | 欧美综合一区 | 国产最新网址 | 国产免费一区二区 |