在寻找为什么我的程序中的 double AveragePrice 为 NaN 时,我偶然发现了 Visual Studio 代码调试器的这种奇怪行为。调试器显示averagePrice = 0,而double.IsNaN为true并且GODOT.GD.Print(averagePrice)打印NaN。
if (double.IsNaN(averagePrice))
{
Godot.GD.Print(averagePrice);
}
我在 Visual Studio 中检查了我的代码作为控制台应用程序进行验证,但 Viusal Studio 似乎是正确的。我唯一的猜测是 Godot 扩展导致了这个问题:
我不确定是否可以提供帮助,但任何有关如何解决此问题的想法将不胜感激。谢谢!
编辑:供参考,并且由于同一变量发生另一个错误,这里是完整的方法:
public void ResolveOffers(Item commodity)
{
int demand = 0;
int supply = 0;
int unitsSold = 0;
double averagePrice = 0;
foreach (ProfessionType professionType in EarningsByProfession.Keys)
{
EarningsByProfession[professionType] = 0;
}
bool hasDemand = _bidsForCommodity.ContainsKey(commodity);
if (hasDemand)
if (_bidsForCommodity[commodity].Count <= 0)
hasDemand = false;
bool hasSupply = _asksForCommodity.ContainsKey(commodity);
if (hasSupply)
if (_asksForCommodity[commodity].Count <= 0)
hasSupply = false;
if (hasDemand && hasSupply)
{
List<Bid> bids = _bidsForCommodity[commodity];
List<Ask> asks = _asksForCommodity[commodity];
foreach (Bid bid in bids)
{
demand += (int)bid.Quantity; //needs to be extracted so that the reports can note it even if no trades happen
}
foreach (Ask ask in asks)
{
supply += (int)ask.Quantity;
}
bids.Shuffle();
asks.Shuffle();
bids.Sort((x, y) => y.Price.CompareTo(x.Price));
asks.Sort((x, y) => x.Price.CompareTo(y.Price));
while (bids.Count > 0 && asks.Count > 0)
{
Bid buyer = bids[0];
Ask seller = asks[0];
if(seller.Agent.UniqueID == 35 && commodity == Item.Food)
{
int cycles = Main.Cycles;
}
double clearingPrice = (buyer.Price + seller.Price) / 2;
uint quantityTraded = Math.Min(buyer.Quantity, seller.Quantity);
buyer.Quantity = MaximumAffordableAmount(quantityTraded, clearingPrice, buyer.Agent.Currency);
if (quantityTraded > 0)
{
double totalTransactionPrice = clearingPrice * quantityTraded;
unitsSold += (int)quantityTraded;
averagePrice += totalTransactionPrice;
if(double.IsNaN(averagePrice))
Godot.GD.Print("Ohje!");
if(averagePrice < 1)
Godot.GD.Print("Auch sehr Ohje!");
buyer.Quantity -= quantityTraded;
seller.Quantity -= quantityTraded;
buyer.QuantityTraded = quantityTraded;
seller.QuantityTraded = quantityTraded;
buyer.ClearingPrice = clearingPrice;
seller.ClearingPrice = clearingPrice;
if (!EarningsByProfession.ContainsKey(buyer.Agent.Profession.Type))
EarningsByProfession.Add(buyer.Agent.Profession.Type, -1 * totalTransactionPrice);
EarningsByProfession[buyer.Agent.Profession.Type] -= totalTransactionPrice;
if (!EarningsByProfession.ContainsKey(seller.Agent.Profession.Type))
EarningsByProfession.Add(seller.Agent.Profession.Type, totalTransactionPrice);
EarningsByProfession[seller.Agent.Profession.Type] += totalTransactionPrice;
seller.PercentOfOrderUnfilled = 1 - (double)seller.QuantityTraded/(buyer.Quantity + buyer.QuantityTraded);
buyer.PercentageOfOfferFilled = (double)buyer.QuantityTraded/(seller.Quantity + seller.QuantityTraded); // (double)seller.QuantityTraded/(buyer.Quantity + buyer.QuantityTraded);
seller.Agent.RemoveFromInventory(commodity, quantityTraded);
seller.Agent.AddCurrency(clearingPrice * quantityTraded); // totalTransactionPrice
buyer.Agent.AddToInventory(commodity, quantityTraded);
buyer.Agent.RemoveCurrency(clearingPrice * quantityTraded);
// _marketData.UpdateData(demand, supply, unitsSold, averagePrice/unitsSold, commodity);
_marketDataContainer.Write(commodity, demand, supply, unitsSold, averagePrice/unitsSold);
buyer.Agent.PriceUpdateFromBid(buyer);
seller.Agent.PriceUpdateFromAsk(seller);
}
if (seller.Quantity == 0)
asks.Remove(seller);
if (buyer.Quantity == 0)
bids.Remove(buyer);
}
ResolveRejectedAsks(asks);
ResolveRejectedBids(bids);
}
else
{
int testint = Main.Cycles;
}
if (double.IsNaN(averagePrice))
{
Godot.GD.Print(averagePrice);
Godot.GD.Print(averagePrice == 0);
}
if (unitsSold > 0)
averagePrice /= unitsSold;
else
averagePrice = 0;
if (double.IsNaN(averagePrice))
{
}
if (Main.Cycles >= 2)
{
}
_marketDataContainer.Write(commodity, demand, supply, unitsSold, averagePrice);
//_marketData.UpdateData(demand, supply, unitsSold, averagePrice, commodity);
/*
else if (_bidsForCommodity.ContainsKey(commodity))
{
List<Bid> bids = _bidsForCommodity[commodity];
ResolveRejectedBids(bids);
}
else if (_asksForCommodity.ContainsKey(commodity))
{
List<Ask> asks = _asksForCommodity[commodity];
ResolveRejectedAsks(asks);
}*/
}
我还注意到,这个片段
double totalTransactionPrice = clearingPrice * quantityTraded;
unitsSold += (int)quantityTraded;
averagePrice += totalTransactionPrice;
当我逐行调试时,不会更新“averagePrice”值。在这种情况下它保持为零,即使“totalTransactionPrice”不是。当我在没有事先断点的情况下运行程序时,它似乎可以很好地计算平均价格。我没看懂。
顺便说一句,抱歉造成混乱。我懒得清理它,因为它现在已经被扔掉了。