code为表现不同的方式在释放VS调试模式 [英] Code is behaving differently in Release vs Debug Mode
问题描述
我们在释放模式VS调试模式下运行时失败的一些单元测试。如果我调试器附加在释放模式测试通过。有太多的code在这里发布,所以我真的只是寻找最佳做法的调试版本模式的问题。我已经检查了:
- 在调试和发布preprocessor指令,但我没有发现任何。
- 条件方法
解决方案:在这种情况下,那是因为我是为平等的比较浮点变量。所以我加了一个扩展方法,我不能改变的花车,以十进制没有出现大的重构:
公共静态类FloatExtension
{
公共静态布尔AlmostEquals(这个浮动的F1,F2浮动,浮动precision)
{
返程(Math.Abs(F1 - F2)< = precision);
}
公共静态布尔AlmostEquals(这个浮动的F1,F2浮动)
{
返回AlmostEquals(F1,F2,.00001f);
}
公共静态布尔AlmostEquals(本浮动?F1,浮?F2)
{
如果(f1.HasValue&安培;&安培; f2.HasValue)
{
返回AlmostEquals(f1.Value,f2.Value);
}
否则,如果(F1 == NULL和放大器;&安培; F2 == NULL)
{
返回true;
}
返回false;
}
}
由于它似乎是浮点相关的有这么多事情可能出错。看到: <一href="http://stackoverflow.com/questions/2461319/c-inconsistent-math-operation-result-on-32-bit-and-64-bit">http://stackoverflow.com/questions/2461319/c-inconsistent-math-operation-result-on-32-bit-and-64-bit 和 <一href="http://stackoverflow.com/questions/566958/double-$p$pcision-problems-on-net">http://stackoverflow.com/questions/566958/double-$p$pcision-problems-on-net
有可浮动点被捣毁这么多东西。而比较彩车平等是一个普遍的禁忌。你chould检查差别不是一个合理的小量小。
We have some unit tests that fail when run in Release mode vs debug mode. If I attach a debugger in release mode the tests pass. There is way too much code to publish here so I am really just looking for best practices in debugging Release mode issues. I have checked for:
- DEBUG and RELEASE preprocessor directives but I did not find any.
- Conditional Methods
SOLUTION: In this case it is because I was comparing floating point variables for equality. I could not change the floats to decimal without a major refactoring so I added an extension method:
public static class FloatExtension
{
public static bool AlmostEquals(this float f1, float f2, float precision)
{
return (Math.Abs(f1 - f2) <= precision);
}
public static bool AlmostEquals(this float f1, float f2)
{
return AlmostEquals(f1, f2, .00001f);
}
public static bool AlmostEquals(this float? f1, float? f2)
{
if (f1.HasValue && f2.HasValue)
{
return AlmostEquals(f1.Value, f2.Value);
}
else if (f1 == null && f2 == null)
{
return true;
}
return false;
}
}
Since it seems to be floating point related there are so many things that can go wrong. See: http://stackoverflow.com/questions/2461319/c-inconsistent-math-operation-result-on-32-bit-and-64-bit and http://stackoverflow.com/questions/566958/double-precision-problems-on-net
There are so many things that can be trashed with floating points. And comparing floats for equality is a general no-no. You chould check the difference smaller than a reasonably epsilon.
这篇关于code为表现不同的方式在释放VS调试模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!