最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

integer - C# when deploid as nuget package a simple calculation gives wrong results - Stack Overflow

programmeradmin2浏览0评论

The following function

public int GetNumber()
{
    int num = (condition1 ? 3 : (condition2 ? 1 : 2));
    int num2 = (condition3 ? 6 : (condition4 ? 3 : 0));
    return num + num2;
}

returns the correct result when run in tests. But when I include it in a NuGet package and reference it in another project it sometimes gives wrong results, e.g. for condition 1 and 2 it sets num = 1 and condition 3 and 4 sets num2 = 0 which is correct but it returns 4 and I have no clue why it does that.

I tried to set a variable in between but VS optimizes it by removing that variable during the build. The code above is somewhat what build has made. I have set breakpoints in the code trying to find out the problem.

The original code is

      public int Get9Box(Measurements measurements, Parameters parameters)
      {
         int bathBox = 0;
         int liquidusBox = 0;
         if (measurements.CurrentBathTemperature > parameters.BathTargetTemperature + parameters.DeadBand)
         {
            bathBox = 3;
         }
         else if (measurements.CurrentBathTemperature < parameters.BathTargetTemperature - parameters.DeadBand)
         {
            bathBox = 1;
         }
         else
         {
            bathBox = 2;
         }

         if (measurements.AverageLast4Liquidus > parameters.LiquidusTargetTemperature + parameters.DeadBand)
         {
            liquidusBox = 6;
         }
         else if (measurements.AverageLast4Liquidus < parameters.LiquidusTargetTemperature - parameters.DeadBand)
         {
            liquidusBox = 0;
         }
         else
         {
            liquidusBox = 3;
         }
         int result = bathBox + liquidusBox;
         return result;
      }
发布评论

评论列表(0)

  1. 暂无评论