C# 为什么 double.NaN 不等于自身?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1145443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:18:11  来源:igfitidea点击:

Why is double.NaN not equal to itself?

c#.net

提问by Carlo

Can someone explain this to me? In C# double.NaN is not equal to double.NaN

谁可以给我解释一下这个?在 C# 中 double.NaN 不等于 double.NaN

bool huh = double.NaN == double.NaN; // huh = false
bool huh2 = double.NaN >= 0; // huh2 = false
bool huh3 = double.NaN <= 0; // huh3 = false

What constant can I compare to a double.NaN and get true?

我可以将什么常数与 double.NaN 进行比较并实现?

采纳答案by Erich Mirabal

If you are curious, this is what Double.IsNaNlooks like:

如果你很好奇,这Double.IsNaN看起来像:

public static bool IsNaN(double d)
{
    return (d != d);
}

Funky, huh?

时髦吧?

回答by Adrian Godong

回答by Francis B.

bool isNaN = Double.IsNaN(yourNumber)

回答by arul

There's a specialized function for this:

有一个专门的功能:

double.IsNan(huh);

回答by David

Use the method "Double.IsNaN( value )" to check for this condition.

使用方法“Double.IsNaN(value)”来检查这种情况。

回答by Colin Mackay

Use Double.IsNan()to test for equality here. The reason is that NaN is not a number.

在这里使用Double.IsNan()测试相等性。原因是 NaN 不是数字。

回答by Mike Dinescu

The behavior is on purpose. The reason being NaN represents something that is not a numberand so that is sort of a catch-all for many things.

这种行为是故意的。原因是 NaN 代表的东西不是数字,所以这对许多事情来说都是一种包罗万象的东西。

The proper way to compare something to being NaN is to use the IsNaNfunction.

将某物与 NaN 进行比较的正确方法是使用IsNaN函数。

回答by Torsten Marek

Actually, you already found the way to check if a IEEE-754 floating point number is NaN: it is the only floating point value (or range of values, because there are several NaNs) that evaluates to Falseif compared to itself, i.e. :

实际上,您已经找到了检查 IEEE-754 浮点数是否为NaN 的方法:它是唯一的浮点值(或值的范围,因为有几个 NaN),False如果与自身进行比较,即:

bool isNaN(double v) {
    return v != v;
}

Under the hood, the Double.IsNaN method might actually do the same thing. You should still use it, because the behavior is quite surprising to anybody who does not know about the FP standard.

在幕后,Double.IsNaN 方法实际上可能会做同样的事情。您仍然应该使用它,因为对于不了解 FP 标准的任何人来说,这种行为都非常令人惊讶。

回答by Michael Meadows

The only thing that we know about NaN is that it's "Not a Number." That doesn't mean that it has a value that is associable with its state. For example:

我们对 NaN 的唯一了解是它“不是数字”。这并不意味着它具有与其状态相关联的值。例如:

∞ + (-∞) = NaN

∞ + (-∞) = NaN

0/0 = NaN

0/0 = NaN

(∞ + (-∞)) <> (0/0)

(∞ + (-∞)) <> (0/0)

Here's some C# to demonstrate

这里有一些 C# 来演示

var infinity = 100d / 0;
var negInfinity = -100d / 0;

var notANumber = infinity + negInfinity;
Console.WriteLine("Negative Infinity plus Infinity is NaN: {0}", double.IsNaN(notANumber));

var notANumber2 = 0d / 0d;
Console.WriteLine("Zero divided by Zero is NaN: {0}", double.IsNaN(notANumber2));

Console.WriteLine("These two are not equal: {0}", notANumber == notANumber2);

回答by Artru

The reason of Double.NaN != Double.NaNis simple:

原因Double.NaN != Double.NaN很简单:

Do you expect 0/0to be the same as Math.Sqrt(-3)? And same as Math.Sqrt(-7)?

你希望0/0和你一样Math.Sqrt(-3)吗?和一样Math.Sqrt(-7)吗?

There is a bug in C# in my opinion where Equals()is not overridden for NaN.

在我看来,C# 中有一个错误,Equals()没有被 NaN 覆盖。

Assert.IsTrue(Double.NaN != Double.NaN);
Assert.IsTrue(Double.NaN.Equals(Double.NaN));

At the same time

同时

Assert.IsTrue(Double.PositiveInfinity == Double.NegativeInfinity);
Assert.IsTrue(Double.PositiveInfinity.Equals(Double.PositiveInfinity));
// same for Double.NegativeInfinity and Single

Use static functions for Doubleand Single, e.g.

Doubleand使用静态函数Single,例如

Double.IsNaN(value) && Double.IsInfinity(value);

Or more specific:

或者更具体的:

Double.IsPositiveInfinity(value);
Double.IsNegativeInfinity(value);