While searching for why the double averagePrice in my program was NaN I stumbled upon this weird behaviour for the Visual Studio Code Debugger. The Debugger shows averagePrice = 0 whereas double.IsNaN is true and GODOT.GD.Print(averagePrice) prints NaN.
if (double.IsNaN(averagePrice))
{
Godot.GD.Print(averagePrice);
}
I checked back my code in Visual Studio as a Console Application for verification, but Viusal Studio seems to get it right. My only guess is that the Godot Extension causes this issue:
Visual Studio Code extensions used
I am not sure if help is possible, but any idea on how to fix this would be appreciated. Thank you!
Edit: For Reference and since there is another error happening with the same variable, here is the full method:
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);
}*/
}
I also noticed, that this snippet
double totalTransactionPrice = clearingPrice * quantityTraded;
unitsSold += (int)quantityTraded;
averagePrice += totalTransactionPrice;
does not update the "averagePrice" value, when I debug line by line. It stays zero in this case, even though "totalTransactionPrice" is not. When I run the program without a prior breakpoint it seems to calculate the averagePrice fine. I don't unserstand.
Sorry for the mess btw. I didn't bother to clean it as it's throwaway by now.
Turns out I was being dumb. Initially I assigned averagePrice in the Watchlist in Visual Studio Code and while experimenting because it didn't seem to work the way I expected I've done this piece of art:
I've deleted this and now it behaves properly. Probably should have been averagePrice == 0 to begin with.