私は興味深い問題に遭遇しました。私はハンバーガーを販売するアプリケーションを構築しています。 JUnitでの私のテストは今まで通ります。しかし、私は、jUnitの結果が実際に同じ数字の印刷バージョンと一致しないことに気付きました。検証済みのテストの倍数がout.prinlnのバージョンと一致しません
さらに、私の計算の結果は、かなり簡単な2倍の加算や減算のような単純な操作から来ています。 11.90の代わりに11.8999999999?
public void completeTransaction(){
double value = 0.0;
for(Burger product : this.transaction.keySet()){
for(int i=0; i<(int)(this.transaction.get(product)); i++){
double price = calculateBurgerPrice(product);
this.income += (double) price;
value += price;
this.soldBurgers += 1;
}
}
System.out.println("Income so far is " + getIncome() + ". Transaction
value: " + value);
this.processedTransactions += 1;
}
ありがとうテスト方法
Income so far is 11.899999999999999. Transaction value: 11.899999999999999
Income so far is 20.9. Transaction value: 9.0
テスト済みメソッドの結果を印刷
@Test
public void canGenerateAndCountProccessedTransactions2(){
till1.newTransaction();
till1.addProduct(burger1);//3.00
till1.addProduct(burger2);//4.60
till1.addProduct(burger3);//4.30
till1.completeTransaction();
assertEquals(11.90, till1.getIncome(), 0.1);
till1.newTransaction();
till1.addProduct(burger1);//3.00
till1.addProduct(burger1);//3.00
till1.addProduct(burger1);//3.00
till1.completeTransaction();
assertEquals(20.90, till1.getIncome(), 0.1);
assertEquals(2, till1.numOfProcessedTransactions());
}
を渡し
テストファイル!
11.90は、ダブルとビットの有限数で提示することはできません。これは、浮動小数点値が(負の)べき乗の合計であるためです.1つは 'new BigDecimal(" 11.90 ")'のようにBigDecimalを使用するか、または丸めと丸めを続ける必要があります。 –
私はそれについてもっと読む必要があります。私はそれを行こう!ありがとう:) – bwielk
'assertEquals(20.90、till1.getIncome()、0.1)'は非常に大きなデルタを持っています。これは小数点の後の最初の桁でさえ気にしないことを意味します。それは1.25と1.5を等しく扱います。あなたはアサーションが失敗することなくそれをもっと小さくすることができます。 –