我有一個場景,我有一個包含3個罐子的標準盒子。出於顯示和查詢的目的,我必須按照其標準配置的十進制數進行報告。 這是不可能說的3罐1組中,2罐1箇中...等
當試圖計算一個盒子的分數時十進制精度損失
例如,最初我將有3罐1盒
我然後除去1個可以得到在0.66重複的3罐
框我然後除去1更能夠導致0.33重複的3罐
框然後我移除最終罐導致框的3 0.0000000000000000000000000001罐
當我刪除最後的罐頭時,我想要的值是0盒3罐,因爲現在每個罐頭都已經從原盒中取出。我意識到,由於在處理有限數量的比特時不能表示0.33,所以存在精度損失。
問題:對於那些在需要使用四捨五入的系統中有更多經驗的人(可能有財務),我有什麼方法可以解決這個問題?如何去除最後一個可能意味着該框不再存在?
編輯:
最後。我使用了Loren Pechtel的建議,然後存儲罐的數量,然後當我需要顯示多少個標準框時,我將罐的總數除以標準框中的罐數,仍然給出遞歸結果,但這對於報告事物的一面。
下面是一些代碼,我希望將在概述問題多一些幫助: -
static void Main(string[] args)
{
var box = new StandardBox(3);
var boxDetail = new BoxDetail(1.0m, box);
var allBoxes = new AllBoxes();
allBoxes.AddBox(boxDetail);
allBoxes.RemoveItemFromBox(boxDetail, 1.0m);
Console.WriteLine(allBoxes);
allBoxes.RemoveItemFromBox(boxDetail, 1.0m);
Console.WriteLine(allBoxes);
allBoxes.RemoveItemFromBox(boxDetail, 1.0m);
Console.WriteLine(allBoxes);
Console.ReadLine();
}
}
public class StandardBox
{
private decimal _quantity;
public StandardBox(decimal quantity){_quantity = quantity;}
public decimal Quantity {get { return _quantity; }}
}
public class BoxDetail
{
private decimal _quantity;
private StandardBox _box;
public BoxDetail(decimal quantity, StandardBox box)
{
_quantity = quantity;
_box = box;
}
public void RemoveItem(decimal quantity)
{
var newQuantity = quantity/_box.Quantity;
_quantity = _quantity - newQuantity;
}
public override string ToString()
{
return _quantity.ToString() + " of " + _box.Quantity.ToString();
}
}
public class AllBoxes
{
private List<BoxDetail> allBoxes = new List<BoxDetail>();
public AllBoxes(){}
public void AddBox(BoxDetail box){allBoxes.Add(box);}
public void RemoveItemFromBox(BoxDetail box, decimal quantity)
{
var boxDetailLineToModify = allBoxes.Find(b => b.Equals(box));
boxDetailLineToModify.RemoveItem(quantity);
}
public override string ToString()
{
var results = string.Empty;
foreach (var box in allBoxes)
{
results += box.ToString() + "\n";
}
return results;
}
}
您可以使用infiteite-precision算術庫。 – SLaks 2012-03-02 22:54:51
使用雙精度而不是十進制。 – jacknad 2012-03-02 22:58:37
我最初嘗試了兩次,並刪除第三罐的結果是1.11022302462516E-16 of 3 – lostinwpf 2012-03-02 23:05:17