Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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. <code>double</code> and <code>float</code> use a completely different representation system (IEEE 754) which reserves some special bit patterns for NaN, +Infinity, -Infinity, ...</p> <p>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 <code>int</code> types as they are exact and don't have a precision error.</p> <p><code>decimal</code> is designed to be used for "real-world" decimal floating point numbers. It's rarely subject to calculations that <code>double</code> and <code>float</code> are designed to do. What would <code>NaN</code> express for a real world number?</p> <p>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 <em>right way</em> 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.</p> <h3>EDIT (very common misconception about decimal):</h3> <p><a href="http://msdn.microsoft.com/en-us/library/system.decimal.aspx" rel="noreferrer">As also noted by MSDN</a>, <code>decimal</code> is not fixed point. It is a floating point number:</p> <blockquote> <p>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. </p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload