VS Code调试器将NaN的double显示为0且调试变量不更新求助
调试异常:VS Code中Godot项目的double变量显示与实际值不符
核心问题与现象
- 排查
averagePrice(double类型)出现NaN的问题时,发现VS Code调试器显示该变量值为0,但实际double.IsNaN(averagePrice)返回true,Godot.GD.Print输出NaN。相关代码片段:if (double.IsNaN(averagePrice)) { Godot.GD.Print(averagePrice); } - 同样的逻辑在Visual Studio控制台应用中运行正常,推测异常由Godot扩展导致。
- 额外异常:逐行调试时,以下代码中
averagePrice += totalTransactionPrice语句未更新averagePrice的值(totalTransactionPrice非0,但averagePrice仍为0),但无断点运行时计算完全正常:double totalTransactionPrice = clearingPrice * quantityTraded; unitsSold += (int)quantityTraded; averagePrice += totalTransactionPrice;
完整方法代码参考
附上出现问题的ResolveOffers方法完整代码:
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); }*/ }
求助
希望能得到解决该调试异常问题的建议。
内容的提问来源于stack exchange,提问作者Sascha
相关产品推荐
相关产品推荐

