Internals of Decimal data type

  • Thread starter Thread starter Michael A. Covington
  • Start date Start date
M

Michael A. Covington

Microsoft's documentation is a bit unclear, but is Decimal in fact a
radix-100 (base-100) arithmetic data type (in which each byte ranges only
from 0 to 99)?

It should be straightforward to dump some Decimal values onto a binary file
(or into a stream that writes into an array of bytes) and examine them.
 
Michael A. Covington said:
Microsoft's documentation is a bit unclear, but is Decimal in fact a
radix-100 (base-100) arithmetic data type (in which each byte ranges only
from 0 to 99)?

No. It's a 96-bit integer, a sign bit, and an exponent in the range
0-28. (The exponent is used to divide, not multiply the mantissa - in
other words, an exponent of 28 shows a much smaller number than an
exponent of 0 (for the same mantissa)).
It should be straightforward to dump some Decimal values onto a binary file
(or into a stream that writes into an array of bytes) and examine them.

You don't even need to do that - just use Decimal.GetBits.

See http://www.pobox.com/~skeet/csharp/decimal.html for a bit more
information.
 
Jon Skeet said:
No. It's a 96-bit integer, a sign bit, and an exponent in the range
0-28. (The exponent is used to divide, not multiply the mantissa - in
other words, an exponent of 28 shows a much smaller number than an
exponent of 0 (for the same mantissa)).

See http://www.pobox.com/~skeet/csharp/decimal.html for a bit more
information.

Actually, that page says it's radix-10 but then doesn't go into the details
of it. I plan to explore a little. More news soon.

Thanks!
 
You're right, and to put it even more clearly:

The Decimal type is a binary integer stored with an offset that represents a
power of 10, and not normalized.

Thus 1 and 1.0 are different numbers. The latter is stored as 10 offset 1
decimal place to the right.

This combines the speed of binary arithmetic with the ability to represent
decimal numbers exactly, and, indeed, to remember how many decimal places
were given (so if you give a price as $1.00 it remains 1.00, not 1 or 1.0).

Clever... but poorly documented!
 
Michael A. Covington said:
You're right, and to put it even more clearly:

The Decimal type is a binary integer stored with an offset that represents a
power of 10, and not normalized.

Thus 1 and 1.0 are different numbers. The latter is stored as 10 offset 1
decimal place to the right.

This combines the speed of binary arithmetic with the ability to represent
decimal numbers exactly, and, indeed, to remember how many decimal places
were given (so if you give a price as $1.00 it remains 1.00, not 1 or 1.0).

Clever... but poorly documented!

Not particularly poorly documented, really - the docs for
System.Decimal are pretty clear about all but the normalisation:

<quote>
The binary representation of an instance of Decimal consists of a 1-bit
sign, a 96-bit integer number, and a scaling factor used to divide the
96-bit integer and specify what portion of it is a decimal fraction.
The scaling factor is implicitly the number 10, raised to an exponent
ranging from 0 to 28.
</quote>

That, to me, can't easily be misinterpreted in the way that your first
post suggests where each byte is between 0 and 99.
 
Back
Top