Javaでは可能性の重複:次のコード与え
Floating point arithmetic not producing exact results in Java
Floating point inaccuracy examplesダブルマイナスのint与える予期しない結果
、:最初の計算の後
double amount = scan.nextDouble();
tenBills = (int)amount/(int)10;
amount = amount - (tenBills * 10);
fiveBills = (int)amount/(int)5;
amount = amount - (fiveBills * 5);
、与えられたsa y 16の金額は6.66になります。しかし、2回目の計算後は1.6600000000000001となります。私はなぜdoubleからintを減算するとそのような結果が生じるのか分からない。
これは[浮動小数点演算](http://en.wikipedia.org/wiki/Floating-point_arithmetic)と呼ばれ、この質問は**常にそうです**。これを読んでください:http://csharpindepth.com/Articles/General/FloatingPoint.aspx –
あなたがあなたの直接の質問への答えを得たら、あなたは[BigDecimal](http://docs.oracle.com/ javase/7/docs/api/java/math/BigDecimal.html)クラスを使用すると、コードの精度が向上します。 – joschi
..または結果を丸める。 –