Java (JDK 1.4) での 10 進数の管理に問題があります。
最初と2番目に2つの二重数値があります(フォーマットされたStringの出力として)。拳と秒の合計を計算すると、10 進数の桁数が多い数値が得られます。
final double first=198.4;//value extract by unmodifiable format method
final double second=44701.2;//value extract by unmodifiable format method
final double firstDifference= first+second; //I receive 44899.598 instead of 44899.6
final double calculatedDifference=44900.1; // comparison value for the flow
final double error=firstDifference-calculatedDifference;// I receive -0.50390605 instead 0.5
if(Math.abs(error)<=0.5d)){
//I must enter in this branch but the error not allows the correct flow!!!
}
/***
* the flow of program is uncorrect and there's a very visible bug in business view
*/
同様の状況では安全ではないため、しきい値(0.5d )を大きくしないことを好みます(コーディングを開始したとき、仕様では比較値として0.1dについて話していました)。それが唯一の解決策である場合、この問題に対して0.9dの値が最も安全な値ですか?
どうすればこの状況を解決できますか? この問題は double 変数の使用によって派生すると考えましたが、float でも同じ問題があります。
いくつかのアイデア(可能であれば、テスト済みのコード行があります;))?