debugging Visual Studio代码编译器显示0表示双精度型NaN

lzfw57am  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(263)

在寻找为什么我的程序中的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扩展导致了这个问题:
Visual Studio Code extensions used
我不确定是否可以提供帮助,但任何关于如何解决这个问题的想法都将不胜感激。谢谢!
Edit:作为参考,由于同一个变量发生了另一个错误,这里是完整的方法:

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”不是。当我运行程序而没有预先断点时,它似乎可以很好地计算averagePrice。我不理解。
顺便说一句,很抱歉这里很乱。我没有费心去清理它,因为它现在已经被扔掉了。

wlp8pajw

wlp8pajw1#

原来我是愚蠢的。最初,我在Visual Studio Code的Watchlist中分配了averagePrice,在实验过程中,因为它似乎没有按照我预期的方式工作,所以我做了这件艺术品:


的数据
我已经删除了这个,现在它的行为正常。可能应该是averagePrice == 0到开始。

相关问题