Bruce Wood <brucewood@canada.com> wrote:[color=blue]
> I don't think that you can talk about decimal or double being more or
> less accurate one than the other. They're very different
> representations. My point was not that decimal is less accurate than
> double, or vice versa, but that they represent values differently and
> so conversions between the two may lose information.[/color]
I agree that they're different, but not quite as different as you seem
to think.
Thinking about it a bit, I *suspect* that all doubles within the
decimal range can be exactly represented with a decimal (due to the
base of decimal including the base of double as a factor), but I'd need
to go through the maths to check.
[color=blue]
> To answer your question, the C# language spec states that decimal is a
> 128-bit value, so its precision is limited. In particular, with
> decimal, the larger the value you try to store the fewer digits you
> have after the decimal place. This is not the case with double.[/color]
Yes it is - if you store a very large number in a double, you'll get
very little precision in absolute terms. When you get to *really* large
numbers, you don't even get *integer* precision.
[color=blue]
> Double, however, has less precision overall than decimal. A double is
> only a 64-bit value, so it runs out of digits of precision much more
> quickly. However, you can represent huge values and still have the same
> number of digits of precision as with values near 1.[/color]
Same number of digits of precision, but not the same number of digits
*after the decimal place*. Big difference. (Decimal still has the same
number of digits of precision with large numbers as with small numbers
too.)
The same is true for decimal though - it always has 28/29 digits of
precision, however large the number is. Double will always have 15/16
digits of precision (IIRC - around that, anyway).
This shouldn't be surprising, as the size of the mantissa stays the
same throughout the range - 52 bits for double, 96 bits for decimal.
(Normalisation gives double an extra implicit bit of precision for most
double values, but that's a bit of a side issue.)
The big difference between the two types is the range of exponents
which are available - decimal keeps the decimal point within the
integer represented by the mantissa, or *just* to one end of it. Double
allows it to be miles away (in either direction), letting you represent
much bigger and much smaller numbers - but with that smaller mantissa.
(There's no particular reason why decimal couldn't represent exponents
with 7 full bits, rather than just most of 5 bits, but it's probably
not appropriate for most uses of decimal.)
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too