Math.Max vs Enumerable.Max [英] Math.Max vs Enumerable.Max
问题描述
Math.Max(1f, float.NaN) == NaN
new[] { 1f, float.NaN }.Max() == 1f
为什么?
编辑:同样的问题双重!
same issue with double also!
推荐答案
正如其他人发布的那样,我推荐了一个一种为什么 - 因为它使用 IComparable
记录在案。
As others have posted, I tweeted one sort of "why" - in that it's using IComparable
as documented.
只是导致另一个为什么。特别是:
That just leads to another "why" though. In particular:
Console.WriteLine(Math.Max(0, float.NaN)); // Prints NaN
Console.WriteLine(0f.CompareTo(float.NaN)); // Prints 1
第一行表示NaN被认为大于0.第二行表明0被认为大于NaN。 (这两个都不能报告这个比较没有意义的结果。)
The first line suggests that NaN is regarded as being greater than 0. The second line suggests that 0 is regarded as being greater than NaN. (Neither of these can report the result of "this comparison doesn't make sense", of course.)
I have the advantage of seeing all the reply tweets, of course, including these two:
看起来不寻常,但这是正确答案。数组的max()是NaN,如果所有元素都是NaN。参见IEEE 754r。
It may seem unusual, but that's the right answer. max() of an array is NaN iff all elements are NaN. See IEEE 754r.
此外,Math.Max使用IEEE 754r总排序谓词,其中规定了NaN与其他人的相对排序。
Also, Math.Max uses IEEE 754r total ordering predicate, which specifies relative ordering of NaN vs. others.
这篇关于Math.Max vs Enumerable.Max的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!