本文介绍了为什么楠(非数字),仅适用于双打?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个可空小数属性公务舱。第三个属性返回其他两个属性相乘的结果。如果HasValue为两个空类型真的,那么我乘并返回结果。我有返回值的几个选项,如果属性中的一个或两个是空:

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:


  • 返回0

  • 抛出一个异常

  • 返回一个神奇的数字(-1)

  • 返回小数? (编辑 - 见注释)

  • Return 0
  • Throw an exception
  • Return a magic number (-1)
  • Return decimal? (EDIT -- see comments)

我想我的选择之一。将返回NaN的,但我看到,这是唯一可能为双类型。这是为什么?

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?

有关的记录,返回0,使得在这种情况下,最有意义的,这就是我打算做,除非有人有更好的建议。

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.

推荐答案

整型为代表.NET使用补制度。虽然他们可以保留一些位模式为特殊值,他们没有选择。 双击浮动使用完全不同的表达系统(IEEE 754),它保留了南有些特殊的位模式,+无限,-Infinity,...

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. double and float use a completely different representation system (IEEE 754) which reserves some special bit patterns for NaN, +Infinity, -Infinity, ...

原因之一楠和无限价值更有意义浮点运算是操作可以通过零结果划分,不仅是因为除数实际上是零,但因为它太小,由类型来表示。其结果是,如果情况并非如此,你可以有一些有效的计算零异常神秘抛出一个鸿沟。因为它们是准确的,并且没有一个精度误差不会出现这种情况了 INT 类型。

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 int types as they are exact and don't have a precision error.

小数的设计是用于真实世界的十进制浮点数字。这是很少受到了计算双浮动设计做。什么会 NaN的表达了一个真实的世界是多少?

decimal is designed to be used for "real-world" decimal floating point numbers. It's rarely subject to calculations that double and float are designed to do. What would NaN express for a real world number?

离开它单独背后的原因,这是它是什么,没有什么我们可以做这件事的,所以去的最佳途径是使用可空类型(它们被设计来帮助正是这种情况)。这是的解决这个问题的正道。如果你不希望这样做(和例外情况没有任何意义),你应该诉诸于一个神奇的数字解决方案。如果你选择这样做,只要确定它是有效结果的域之外

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 way to 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.

指出,小数不是固定点。它是一个浮点数:

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

一个十进制数为一个浮点值,它包括一个标志,一个数字值,其中每一位在该值的范围从0到9,并指示所述数值的整数和小数部分分离出来的浮动小数点的位置的比例因子。

这篇关于为什么楠(非数字),仅适用于双打?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:49