为什么Assert.AreEqual(1.0,double.NaN,1.0)传递?AreEqual、Assert、NaN、double

由网友(ジ坐在坟头调戏鬼う)分享简介:短的问题,为什么 Assert.AreEqual(1.0,double.NaN,1.0)通过?而 Assert.AreEqual(1.0,double.NaN)失败。难道是MSTest的(Microsoft.VisualStudio.QualityTools.UnitTestFramework)中的错误还是我失去了一些...

短的问题,为什么 Assert.AreEqual(1.0,double.NaN,1.0)通过?而 Assert.AreEqual(1.0,double.NaN)失败。

难道是MSTest的(Microsoft.VisualStudio.QualityTools.UnitTestFramework)中的错误还是我失去了一些东西呢?

最好的问候,埃吉尔。

更新:也许应该补充一点,后面我的问题的原因是,我有一大堆的原因,不幸的是传递给一些线性代数矩阵运算是NaN或(+/-)无限的结果单元测试。单元测试都很好,但由于Assert.AreEqual在双打增量是NaN或无穷我被留下来认为,code我的测试是正确的将通过在实际或/和预期。

解决方案

要小心。楠怪异,有点像在很多的DBMS空的,你不应该(无论是直接或与Assert.AreEqual)比较值的。从文档的 Double.NaN :

  

请使用isNaN来确定是否值   不是数字。这是不可能的,以   确定值是否是一个不   通过比较到另一个号码   等于为NaN。

 双零= 0;
Console.WriteLine((0 /零)== Double.NaN); //打印假
Console.WriteLine(Double.IsNaN(0 /零)); //输出true
 

您不得不在同行断言(双,双,双)的内部,看看发生了什么事情,但在一般情况下,你是取决于相对的NaN不确定的行为。

Short question, why does Assert.AreEqual(1.0, double.NaN, 1.0) pass? Whereas Assert.AreEqual(1.0, double.NaN) fails.

为什么我qq音乐上的we are the world歌词与百度上的不一样

Is it a bug in MSTest (Microsoft.VisualStudio.QualityTools.UnitTestFramework) or am I missing something here?

Best regards, Egil.

Update: Should probably add, that the reason behind my question is, that I have a bunch of unit tests that unfortunately passed due to the result of some linear algebraic matrix operation being NaN or (+/-)Infinity. The unit tests are fine, but since Assert.AreEqual on doubles with a delta will pass when actual or/and expected are NaN or Infinity, I was left to believe that the code I was testing was correct.

解决方案

Be careful. NaN is weird, somewhat like null in many DBMSs, and you shouldn't be comparing values to it (either directly, or with Assert.AreEqual). From the docs for Double.NaN:

Use IsNaN to determine whether a value is not a number. It is not possible to determine whether a value is not a number by comparing it to another value equal to NaN.

double zero = 0;
Console.WriteLine((0 / zero) == Double.NaN);  // prints false
Console.WriteLine(Double.IsNaN(0 / zero));  // prints true

You'd have to peer at the internals of Assert(double, double, double) to see what's going on, but in general, you're depending on undefined behavior relative to NaN.

阅读全文

相关推荐

最新文章