C# 为什么 NaN(不是数字)只能用于双打?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/986268/
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 04:56:52  来源:igfitidea点击:

Why is NaN (not a number) only available for doubles?

c#types

提问by Jamie Ide

I have a business class that contains two nullable decimal properties. A third property returns the result of multiplying the other two properties. If HasValue is true for the two nullable types then I multiply and return the result. I have a few options for the return value if one or both of the properties is null:

我有一个包含两个可为空的十进制属性的业务类。第三个属性返回其他两个属性相乘的结果。如果 HasValue 对于两个可为空的类型为真,那么我将相乘并返回结果。如果一个或两个属性为空,我有几个返回值选项:

  • Return 0
  • Throw an exception
  • Return a magic number (-1)
  • Return decimal? (EDIT -- see comments)
  • 返回 0
  • 抛出异常
  • 返回一个幻数 (-1)
  • 返回十进制?(编辑——见评论)

I thought one of my options would be to return NaN, but I see that this is only possible for the double type. Why is this?

我认为我的选择之一是返回 NaN,但我发现这仅适用于 double 类型。为什么是这样?

For the record, returning 0 makes the most sense in this case and that's what I plan to do unless someone has a better suggestion.

为了记录,在这种情况下返回 0 最有意义,除非有人有更好的建议,否则这就是我计划做的。

采纳答案by Mehrdad Afshari

Integral types in .NET use two's complement system for representation. While they could reserve some bit patterns for special values, they chose not to. doubleand floatuse a completely different representation system (IEEE 754) which reserves some special bit patterns for NaN, +Infinity, -Infinity, ...

.NET 中的整数类型使用二进制补码系统来表示。虽然他们可以为特殊值保留一些位模式,但他们选择不这样做。doublefloat使用完全不同的表示系统 (IEEE 754),该系统为 NaN、+Infinity、-Infinity、...保留了一些特殊的位模式。

One reason that NaN and Infinity values make more sense for floating point arithmetic is that operations could result division by zero, not only because the divisor is actually zero, but because it's too small to be represented by the type. As a result, if that wasn't the case, you could have some valid calculation mysteriously throw a divide by zero exception. This won't happen for inttypes as they are exact and don't have a precision error.

NaN 和 Infinity 值对浮点算术更有意义的一个原因是运算可能导致除以零,不仅因为除数实际上为零,而且因为它太小而无法由类型表示。因此,如果情况并非如此,您可能会在某些有效计算中神秘地抛出除以零异常。这对于int类型不会发生,因为它们是精确的并且没有精度错误。

decimalis designed to be used for "real-world" decimal floating point numbers. It's rarely subject to calculations that doubleand floatare designed to do. What would NaNexpress for a real world number?

decimal旨在用于“真实世界”的十进制浮点数。它很少受制于计算,double并且float旨在这样做。NaN一个真实世界的数字会表达什么?

Leaving reasons behind it alone, it is what it is and there's nothing we could do about it, so the best route to go is to use nullable types (they are designed to help with exactly this kind of situation). It's the right wayto solve this problem. If you don't want to do that (and exceptions don't make sense), you should resort to the magic number solution. If you chose to do so, just make sure it's outside of the domain of valid results.

撇开原因不谈,它就是这样,我们对此无能为力,因此最好的方法是使用可空类型(它们旨在帮助解决这种情况)。这是解决这个问题的正确方法。如果您不想这样做(并且例外没有意义),您应该求助于幻数解决方案。如果您选择这样做,只需确保它不在有效结果的范围内。

EDIT (very common misconception about decimal):

编辑(对十进制非常常见的误解):

As also noted by MSDN, decimalis not fixed point. It is a floating point number:

正如 MSDN 所指出的decimal不是固定点。它是一个浮点数:

A decimal number is a floating-point value that consists of a sign, a numeric value where each digit in the value ranges from 0 to 9, and a scaling factor that indicates the position of a floating decimal point that separates the integral and fractional parts of the numeric value.

十进制数是一个浮点值,由一个符号、一个数字值(该值中的每个数字的范围为 0 到 9)和一个比例因子组成,该比例因子指示分隔整数部分和小数部分的浮点小数点的位置的数值。

回答by Anis Ben Khiroun

To resolve NAN & Infinity use this

要解决 NAN & Infinity 使用这个

if (Double.IsNaN(YourValue) || Double.IsInfinity(YourValue))
{
    YourValue = 0;
}