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

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

问题描述

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

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

是MSTest中的一个错误(Microsoft。 VisualStudio.QualityTools.UnitTestFramework)还是我在这里遗漏的东西?

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

最好的问候,Egil。

Best regards, Egil.

更新:应该补充一点,我的问题背后的原因是,我有一堆单元测试,不幸的是由于一些线性代数矩阵运算的结果是NaN或(+ -)无穷。单元测试很好,但是由于Assert.AreEqual在三角形上加倍,所以当实际或/和预期为NaN或Infinity时,我相信我正在测试的代码是正确的。

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.

推荐答案

小心。 NaN很奇怪,在许多DBMS中有点像null,你不应该比较它的值(直接或者Assert.AreEqual)。从 Double.NaN 的文档:

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:


使用IsNaN来确定值
是否不是数字。
不能通过将值与不同的
值进行比较来确定一个值是不是一个
数字。另一个
值等于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

你必须在Assert的内部对等体(double,double,double )看看发生了什么,但一般来说,你取决于与NaN相关的未定义行为。

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天全站免登陆