michaelgweier <(E-Mail Removed)> wrote:
> Hello! I was working on some code the other day, and I came across an odd
> discrepancy between the decimal and the double type.
>
> If I attempt to divide a decimal by zero, the framework throws an error.
> If I attempt to divide a double by zero, the framework returns infinity.
>
> From a mathematical standpoint, it would seem to me that the decimal handles
> this division correctly, while the double's handling of this is flawed.
>
> Is there a practical reason why these two types handle this so differently?
In a word, standards. At least, I suspect that's the reason.
float/double follow the IEC 60559 standard rules for arithmetic,
including division by zero resulting in an "infinite" value rather than
throwing an exception.
Decimal doesn't (or at least doesn't *have* to - the C# spec allows for
the possibility) support an "infinite" value whereas float/double do.
Similar decimal doesn't have a NaN specified.
--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too