为什么Assert.AreEqual(1.0,double.NaN,1.0)传递? [英] Why does Assert.AreEqual(1.0, double.NaN, 1.0) pass?

查看:212
本文介绍了为什么Assert.AreEqual(1.0,double.NaN,1.0)传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短的问题,为什么 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.

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.

这篇关于为什么Assert.AreEqual(1.0,double.NaN,1.0)传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆