C# 在版本设置中编译会引入无限循环
这个循环是一个简单的寻路算法的一部分。问题是,在发布模式下构建时,程序永远不会离开这个循环。 我们在一个“表”上移动,它是一个二维数组,StepGrid数组保持从起点(pZero)到达每个对应点(表)所需的步数。 当使用发行版和调试设置运行附加到Visual Studio 2012调试器时,此代码工作正常。使用调试设置编译时工作正常。 使用版本设置编译时,此代码不起作用C# 在版本设置中编译会引入无限循环,c#,.net,compiler-optimization,C#,.net,Compiler Optimization,这个循环是一个简单的寻路算法的一部分。问题是,在发布模式下构建时,程序永远不会离开这个循环。 我们在一个“表”上移动,它是一个二维数组,StepGrid数组保持从起点(pZero)到达每个对应点(表)所需的步数。 当使用发行版和调试设置运行附加到Visual Studio 2012调试器时,此代码工作正常。使用调试设置编译时工作正常。 使用版本设置编译时,此代码不起作用 public List<IntPoint> PathFind(IntPoint pZero, IntPoint
public List<IntPoint> PathFind(IntPoint pZero, IntPoint pEnd)
{
float BIGVALUE = 1000000000f;
IntPoint p0 = pZero;
List<IntPoint> res = new List<IntPoint>();
//Initialize StepGrid
StepGrid = new float[TableWidth][];
for (int x = 0; x < StepGrid.Length; x++)
{
StepGrid[x] = new float[TableHeight];
for (int y = 0; y < StepGrid[x].Length; y++)
StepGrid[x][y] = BIGVALUE;
}
List<IntPoint> visitandi = new List<IntPoint>() { p0 };
List<IntPoint> addendi = new List<IntPoint>();
if (p0.X > StepGrid.Length || p0.Y > StepGrid[0].Length ||
pEnd.X > StepGrid.Length || pEnd.Y > StepGrid[0].Length)
return res;
StepGrid[p0.X][p0.Y] = 0;
bool progressMade = true;
while (progressMade)
{
progressMade = false;
addendi.Clear();
for (int cp = 0; cp < visitandi.Count; cp++)
{
float pdist = this.StepGrid[visitandi[cp].X][visitandi[cp].Y];
// PossibleMoves is an array containing all the possible relative moves from a given point. MoveLen contains the steps traveled when performing each one of PossibleMoves
for (int pm = 0; pm < PossibleMoves.Length; pm++)
{
IntPoint p3 = visitandi[cp] + PossibleMoves[pm];
if (CanMoveTo(p3)) //if the pixel is white i can move there
{
float arrivalDist = pdist + MoveLen[pm];
float oldDist = StepGrid[p3.X][p3.Y];
if ( StepGrid[p3.X][p3.Y] > arrivalDist)
{
if (StepGrid[p3.X][p3.Y] >= BIGVALUE)
addendi.Add(p3);
StepGrid[p3.X][p3.Y] = arrivalDist;
progressMade = true;
}
}
}
}
if (addendi.Count > 0)
progressMade = true;
visitandi.AddRange(addendi);
}
.....
}
protected bool CanMoveTo(IntPoint p)
{
//Table is byte[,] and is a bitmap gray scale image
if (p.X < TableWidth && p.Y < TableHeight && p.X > -1 && p.Y > -1)
if (Table[p.X, p.Y] > BrightnessThreshold)//BrightnessThreshold=70
return true;
else
return false;
return false;
}
公共列表路径查找(IntPoint pZero、IntPoint pEnd)
{
float BIGVALUE=1000000000f;
积分p0=pZero;
List res=新列表();
//初始化步进网格
StepGrid=新浮点[TableWidth][];
对于(int x=0;xStepGrid.Length | | p0.Y>StepGrid[0]。Length||
pEnd.X>StepGrid.Length | | pEnd.Y>StepGrid[0].长度)
返回res;
阶梯网格[p0.X][p0.Y]=0;
bool progressMade=true;
虽然(取得了进展)
{
进步=错误;
附录1.Clear();
对于(int cp=0;cpArrivalList)
{
如果(StepGrid[p3.X][p3.Y]>=BIGVALUE)
增编:增编(p3);
StepGrid[p3.X][p3.Y]=到达者;
进步=正确;
}
}
}
}
如果(addendi.Count>0)
进步=正确;
visitandi.AddRange(附录);
}
.....
}
受保护的bool CanMoveTo(输入点p)
{
//表是字节[,],是位图灰度图像
如果(p.X<桌宽和p.Y<桌高和p.X>-1和p.Y>-1)
如果(表[p.X,p.Y]>BrightnessThreshold)//BrightnessThreshold=70
返回true;
其他的
返回false;
返回false;
}
多亏了大家对我的问题进行了评论,我才找到了解决办法。
由于处理器架构以及寄存器中如何处理这些数字,调试和发布之间的浮点数比较有点不一致。
解决方案是在比较中添加一个公差,使这条线
if ( StepGrid[p3.X][p3.Y] > arrivalDist)
应改为
if ( StepGrid[p3.X][p3.Y] - arrivalDist > epsilon)
我们需要仔细分析很多代码。你能不能用一个简短的完整例子来重现这个问题?您执行了哪些诊断来检查每种情况下的不同之处?好的,然后再试一次。当以双精度进行计算时,问题是否重现?CanMoveTo()是什么样子的?你有没有在浮点上做过精确的相等比较,而不是?注意,在调试和发布之间浮点精度有点差,是不是其中一个比较浮点的if语句从来没有足够接近触发匹配?好的,我找到了解决方案。。。。if(StepGrid[pDest.X][pDest.Y]-arrivalist>0.1f)感谢您指出浮点不一致性。事实上,几分钟前有人发布的答案几乎是正确的,我不知道他为什么要删除它。Lasse V.Karlsen和dbc指出了正确的方向。谢谢